简体   繁体   中英

checking checkbox state which is inside Dataitemtemplate devexpress gridview when button is clicked from code behind

I wanto check the state of checkbox which is inside the data item template in devexpress grid view when the user click the ok button here is my aspx code for gridviewColumn

<Columns>
                                    <dx:GridViewDataColumn Caption="Data Source" FieldName="dataSrc" VisibleIndex="1"></dx:GridViewDataColumn>
                                    <dx:GridViewDataColumn Caption="Download" FieldName="dwnloadConfig" VisibleIndex="2" Width="70px" >
                                        <DataItemTemplate>
                                            <dx:ASPxCheckBox ID="cbDwnloadConfig" ClientInstanceName="cbDwnloadConfig
                                                " runat="server" >

                                            </dx:ASPxCheckBox>
                                        </DataItemTemplate>                                        
                                </Columns>

Now when ok button is clicked i want to check the state of cbDwnloadConfig on server side

I used following code on cs file to access the dataitem template control but the checked state is always false.

ASPxCheckBox cbDwnload = gvDataSrc.FindRowCellTemplateControl(rwIndex[i], gvDataSrc.Columns["dwnloadConfig"] as GridViewDataColumn, "cbDwnloadConfig") as ASPxCheckBox;

So what is the proper way to check the state of checkbox which is inside the dataitemtemplate?

It may be that you are binding your grid view in PageLoad method and that may cause this behaviour.

You should bind your grid view in following way

if(!page.IsPostBack)
{
 //BIND YOUR GRID
}

Above will prevent rebinding of gridview when button is clicked.

Well for those of you having the same issue i have found solution to the problem, I had to use callback for this issue.

<Columns>
                <dx:GridViewDataColumn Caption="Data Source" FieldName="dataSrc" VisibleIndex="1"></dx:GridViewDataColumn>
              <dx:GridViewDataColumn Caption="Download" FieldName="dwnloadConfig" VisibleIndex="2" Width="70px" >
                <DataItemTemplate>
                   <dx:ASPxCheckBox ID="cbDwnloadConfig" ClientInstanceName="cbDwnloadConfig" runat="server" >
             <ClientSideEvents CheckedChanged="function(s,e)
                                                    {dwnloadSrc.PerformCallback(s.GetChecked());
                                                    }"></ClientSideEvents>
                 </dx:ASPxCheckBox>
               </DataItemTemplate>                                        
</Columns>

C#

 protected void dwnloadSrc_OnCallback(object source, CallbackEventArgs e)
{
    var param = e.Parameter;
    //now check the paramater
    //and do your magic 
}

you also can pass two or multiple parameter at once from the front end/aspx for that just do this:

dwnloadSrc.PerformCallback(s.GetChecked()+'_'+ gv.FocusedRowsIndex())

and at the Code behind you can check the parameter :

    var param = e.Parameter;
    var newParam = param.Split('_');
    bool state = Convert.ToBoolean(newParam[0]);
    int rwIndex = Convert.ToInt32(newParam[1]);

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM