Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview with radio button and a dropdown list. I wanted to disable and enable the drop down list according to the button selected.

Both the radio button & the dropdown list are templates. I tried rowcommand, but it is not working, means no control coming there.

What I am trying is when the user selecta the "Declain", then only I wanted the dropdown list to be enabled and allow them to select from it.

Here is the code:

XML
<asp:GridView ID="InTransitGridView" runat="server"
                DataSourceID="InTransitItemsObjectDataSource"
                AutoGenerateColumns="False" SkinID="GridViewSkin" Width="100%"
                OnRowCommand="InTransitGridView_RowCommand"
                onselectedindexchanged="InTransitGridView_SelectedIndexChanged">
                <Columns>
                    <asp:BoundField DataField="ponumber" HeaderText="PO Number" />
                    <asp:BoundField DataField="prd_number" HeaderText="Product Code" />
                    <asp:BoundField DataField="prd_name" HeaderText="Product Name" />
                    <asp:BoundField DataField="prd_style" HeaderText="Style" Visible="False" />
                    <asp:BoundField DataField="prd_sub_style" HeaderText="Sub Style"
                        Visible="False" />
                    <asp:BoundField DataField="serial_no" HeaderText="Lot/Serial #" />
                    <asp:BoundField DataField="exp_date" HeaderText="Expiry Date" />
                    <asp:BoundField DataField="quantity" HeaderText="Qty" >
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="Rcvd Qty">
                        <ItemTemplate >
                            <asp:TextBox ID="RcvdQtyTextBox" runat="server" Width="25px" Text='<%# Eval("quantity") %>'></asp:TextBox>
                        </ItemTemplate>
                        <ItemStyle HorizontalAlign="Center" />
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Select">
                       <ItemTemplate>
                          <asp:HiddenField ID="inv_rec_idHiddenField" runat="server" Value='<%# Eval("inv_rec_id") %>' />
                          <asp:HiddenField ID="com_file_idHiddenField" runat="server" Value='<%# Eval("com_file_id") %>' />
                          <asp:RadioButton ID="btn1" Text="Accept" runat="server" Font-Size="Small"
                               GroupName="select" Checked="True" />
                          <asp:RadioButton ID="btn2" Text="Decline" runat="server" Font-Size="Small"
                               GroupName="select" />
                       </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" />
                       <ItemStyle Width="150px" HorizontalAlign="Center" />
                      </asp:TemplateField>
                      <asp:TemplateField HeaderText="Prd Status">
                          <ItemTemplate>
                              <asp:DropDownList ID="ddlUSG_PRD_STATUS" runat="server" >
                              </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
</asp:GridView>


Please help!
Thanks,
Ali
Posted
Updated 25-Aug-10 7:55am
v2

Do the following:

1. Add an OnRowDataBound event to the GridView (say, OnRowDataBound="InTransitGridView_RowDataBound" ) and remove the OnRowCommand and onselectedindexchanged events.

2. Add the following code in the Code Behind (To bind each radio button control with a JavaScript onclick event method):

protected void InTransitGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            RadioButton decline = e.Row.FindControl("btn2") as RadioButton;
            RadioButton accept = e.Row.FindControl("btn1") as RadioButton;
            
            DropDownList ddlStatus = e.Row.FindControl("ddlUSG_PRD_STATUS") as DropDownList;
            if (decline != null && ddlStatus != null && accept != null)
            {
                //Set the CommandArgument to Person's Id for the Link buttons so that, when user clicks the Edit link, the Id value can be retrieved from the CommandArgument
                decline.Attributes["onclick"] = "EnableDisableControl(this,0,'" + ddlStatus.ClientID + "')";
                accept.Attributes["onclick"] = "EnableDisableControl(this,1,'" + ddlStatus.ClientID + "')";
            }
        }
}:


3. In the Markup, define a JavaScript method as follows (that enables or disables the drop-down list based upon the selection):

XML
<script language="javascript">
function EnableDisableControl(radio, isDisable, dropDownListId)
{
        var dropDownList = document.getElementById(dropDownListId);
        if(radio.checked)
        {
            if(isDisable == 0)
            {
                dropDownList.disabled = "disabled";
            }
            else
            {
                dropDownList.disabled = "";
            }
        }
}
</script>


Hopefully, your problem will be solved.

Note: I didn't check the cross-browser issues for the JavaScript, tested only in FireFox. I believe you can handle this.
 
Share this answer
 
v2
Thank you very much Deeksha,
This one saved my day.

thanks again
Ali
 
Share this answer
 
Comments
Al-Farooque Shubho 25-Aug-10 23:03pm    
I wonder why you thanked Deeksha, while Al-Farooque Shubho gave you the answer :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900