asp.net - Preserving CheckBox's Value in Dynamic GridView TemplateField -


I am trying to create a gridview that has a list of users and permission levels in which they will have access to each line The following areas will be: USER_ID, Username, Permission1, Permission2, ..., PermissionN, where permissions are the values ​​of the field if the user has that permission and "1" (Note that they do not have "0" that are returned by DAL Not allowed to call 1 column Has not, but allow the actual name). I want to represent the data by using the check box, allowing an administrator to quickly provide at once or allow a large number of them to cancel the permission.

Since I do not know the number of first hand permissions, I create dynamic TemplateFields and checkboxes, and it works fine; Gridview shows the current approval level of all users. I encounter a problem when I try to update the permissions on the user's check and uncheck box.

By changing user permissions, I have an "update" button, which certainly causes a postback. Since there is a postback before the OnClick event, as long as I reach OnClick, all the checkboxes are reset to their initial values. Is there a way in which I can grab the value of property checked by checkbox before postback? Is there another (better) way to do this? . Thanks

ASPX:

  & lt; ASP: GridView id = "gvUsers" runat = "server" AutoGenerateColumns = "false" EnableModelValidation = "correct" DataKeyNames = "id" & gt; & Lt; / ASP: GridView & gt; & Lt; Asp: ObjectDataSource id = "odsUsers" runat = "server" SelectMethod = "GET_USERS" type name = "ODL" & gt; & Lt; / ASP: ObjectDataSource & gt; & Lt; Asp: button id = "btnUpdate" runat = "server" text = "update permissions" OnClick = "btnUpdate_OnClick" />   

Code Back:

  Private Static String [] Excluded Coles = {"id", "Username"}; Protected Zero Page_Load (Object Sender, EventArgs e) {if (! IsPostBack) // IsPostBack Detection GridView Refresh bindGridViewWithHiddenID (gvUsers, ((DataView) odsUsers.Select ()) when the update button is clicked. Tables, excludeCols); } Public static bindGridViewWithHiddenID (GridView GV, DataTable DT, string [] excludeCols) {gv.Columns.Clear () bool; Gv.DataBind (); If (live! = Null & amp; dt! = Null & amp; dt.rows.ccount> 0) {DataControlField newField; Foreign currency (DataColumn columns in dt.Column) {If (outside calls.column (column.panname)) {newField = new boundfield (); ((Boundfield newfold) .datafield = column. Column name; } And {newField = new TemplateField (); (TemplateField newfield) .ItemTemplate = New custom template (column.ColumnName); } NewField.HeaderText = column.ColumnName; If (column.ColumnName == "ID" || column.ColumnName.EndsWith ("_ ID")) newField.Visible = false; Gv.Columns.Add (Newfield); } Gv.DataSource = dt; Gv.DataBind (); Back true; } return false; } // At the time of execution, the checkbox is already restored to the protected betaapadet tag (object sender, eventArgse E) {...}   

custom template class:

  public class custom template: ITemplate {Private string binding; Private static calculation = 0; Public Custom Template (string colNameBinding) {binding = colNameBinding; } Public Zero Institution (Control Container) {checkbox chk = new checkbox (); Chk.ID = "chk" + Count ++; Chk.DataBinding + = new event heredollar (this.chk_OnDataBinding); Container.Controls.Add (mortgage); } Public Zero chk_OnDataBinding (Object Sender, EventArgs e) {Checkbox chk = (Checkbox) Sender; GridViewRow Naming Resource = (GridViewRow) chk.NamingContainer; DataRowView Detorro = (DataRowView) Naming Container.DataItem; If (Detroor [Binding]. Ostring () == "1") chk.Checked = true; Other chk.Checked = false; }   

This behavior is expected and normal is this page on dynamically added controls To do with the life cycle and viewstate tracking. I read you from Scott Mitchell specifically with section titles: View state and dynamically added control

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

c# - Using Vici cool Storage with monodroid -

python - referencing a variable in another function? -