Dropdownlist value returns 0 when accesing it from nested gridview on button click in asp.net

136 Views Asked by At

So the problem is that, that I have a nested gridview and in childgrid I have a dropdownlist and textboxes. I want to save the values of Dropdownlist and textboxes on button click, so I have tried foreach loop and for loop to access the values of drop down and text boxes and the results where each time I got dropdown value=0 and textbox value with ","(for example: textbox.text="aa" then on backhand it gives ",aa" on button click. Main problem is dropdown selected. NOTE:I don't want to retrieve values on rowcommand or any gridview function


    <asp:GridView runat="server" ID="gv_TrvMainNested" ClientIDMode="Static" AutoGenerateColumns="false" Visible="True"
                        CssClass="table table-striped table-bordered table-hover table-checkable table-responsive "
                        EmptyDataText="No Record Found" OnRowDataBound="gv_TrvMainNested_RowDataBound" DataKeyNames="CRIndex" >
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <img  alt="" style="cursor: pointer" src="../Images/plus.png" />
                                    <asp:Panel ID="pnlOrders" runat="server" Style="display: none">
                                        <asp:GridView runat="server" ID="gv_TrvDetailNested" ClientIDMode="Static" AutoGenerateColumns="false"
                                            EmptyDataText="No Record Found"  CssClass = "ChildGrid" OnRowDataBound="gv_TrvDetailNested_RowDataBound">
                                            <Columns>
                                                <asp:TemplateField HeaderText="Pickup Location">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_PLocation" runat="server" Text='<%# Eval("PickupPoint") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Pickup City">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_PCity" runat="server" Text='<%# Eval("PickupCity") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Pickup Date">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_PDate" runat="server" Text='<%# Eval("PickupDate") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Dropoff Location">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_DLocation" runat="server" Text='<%# Eval("DropoffPoint") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Dropoff City">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_DCity" runat="server" Text='<%# Eval("DropoffDate") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Dropoff Date">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_DDate" runat="server" Text='<%# Eval("DropoffDate") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Hold Car (Hours)">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_HoldHour" runat="server" Text='<%# Eval("HoldHours") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Comments" Visible="false">
                                                    <ItemTemplate>
                                                        <asp:Label ID="lbl_Comment" runat="server" Text='<%# Eval("Comments") %>'></asp:Label>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Select Driver" Visible="false">
                                                    <ItemTemplate>
                                                        <asp:DropDownList ID="gv_DDLDriver" Width="200" CssClass="form-control" AutoPostBack="false" runat="server" DataTextField="DriverName" DataValueField="DriverIndex"></asp:DropDownList>
                                                        <asp:Label ID="ddlValue" runat="server" />
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Vehicle Registration No" Visible="false">
                                                    <ItemTemplate>
                                                        <asp:TextBox ID="tb_Reg" Width="200" CssClass="form-control" runat="server"></asp:TextBox>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                                <asp:TemplateField HeaderText="Remarks" Visible="false">
                                                    <ItemTemplate>
                                                        <asp:TextBox ID="tb_Remark" Width="200" CssClass="form-control" runat="server"></asp:TextBox>
                                                    </ItemTemplate>
                                                </asp:TemplateField>
                                            </Columns>
                                        </asp:GridView>
                                    </asp:Panel>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField ItemStyle-Width="50">
                                <HeaderTemplate>
                                    <label runat="server" class=" m-checkbox m-checkbox--info ">
                                        <asp:CheckBox runat="server" ID="cb_checked" onclick="checkAll(this);" ClientIDMode="Static" />
                                        <span></span>
                                    </label>
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <label class=" m-checkbox m-checkbox--info ">
                                        <asp:CheckBox runat="server" ID="cb_check" ClientIDMode="Static" />
                                        <span></span>
                                    </label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Travel ID">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_CrIndex" runat="server" Text='<%# Eval("CRIndex") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Employee ID">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_EmpId" runat="server" Text='<%# Eval("EmployeeId") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Employee">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Emp" runat="server" Text='<%# Eval("Employee") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Position">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Position" runat="server" Text='<%# Eval("Position") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Department">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Depart" runat="server" Text='<%# Eval("Department") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Contact No">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Contact" runat="server" Text='<%# Eval("CellNo") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Travel Type">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_TrvType" runat="server" Text='<%# Eval("TravelType") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Route Type">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_RouteType" runat="server" Text='<%# Eval("RouteType") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Travel Route">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_TrvRoute" runat="server" Text='<%# Eval("TravelRoute") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Period" Visible="false">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Period" runat="server" Text='<%# Eval("Period") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Status">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Status" runat="server" Text='<%# Eval("Status") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Remarks">
                                <ItemTemplate>
                                    <asp:Label ID="lbl_Remarks" runat="server" Text='<%# Eval("Remarks") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Action">
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnk_View" runat="server" Text="View" CommandName="View" CommandArgument='<%# Eval("CrIndex") %>'></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>``` 
```ASPX.CS: 

        foreach (GridViewRow row in gv_TrvMainNested.Rows)
                                {
                                    GridView gvInner = row.FindControl("gv_TrvDetailNested") as GridView;
                                    foreach (GridViewRow innerrow in gvInner.Rows)
                                    {
                                        var ddl = innerrow.FindControl("gv_DDLDriver") as DropDownList;
                                        var ddlValue = innerrow.FindControl("ddlValue") as Label;
                                        ddlValue.Text = ddl.SelectedItem.Value;
                                    }
                                }
                            ----------OR------------------
    for (int i = 0; i < gv_TrvMainNested.Rows.Count; i++)
                                    {
                                        for (int j = 0; j < ((GridView)gv_TrvMainNested.Rows[i].FindControl("gv_TrvDetailNested")).Rows.Count; j++)
                                        {
                                     var ddl=   ((GridView)gv_TrvMainNested.Rows[i].FindControl("gv_TrvDetailNested")).Rows[j].FindControl("gv_DDLDriver") as DropDownList;
                                        var txtReg = ((TextBox)((GridView)gv_TrvMainNested.Rows[i].FindControl("gv_TrvDetailNested")).Rows[j].FindControl("tb_Reg")).Text;
                                        var txtRemarks = ((TextBox)((GridView)gv_TrvMainNested.Rows[i].FindControl("gv_TrvDetailNested")).Rows[j].FindControl("tb_Remark")).Text;
                                        if (Helpers.ConvertToInt32(ddl.SelectedItem.Value) > 0 && txtReg != "")
             }
    }
    }

1

There are 1 best solutions below

3
Albert D. Kallal On

You don't need nor want a loop for this.

and as you note, you want to avoid the built in events (good for you!!! - you don't need them either, and they are often more trouble then they are worth).

Ok so you have this button in the grid:

          <asp:TemplateField HeaderText="Action">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnk_View" runat="server" Text="View" CommandName="View" CommandArgument='<%# Eval("CrIndex") %>'></asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>

Any reason this is not a regular button we click on? And as you noted, you don't want to use the GV events, so we don't need command Name or argument either, do we? (I guess we can leave them as is).

So, that link button does not seem to have a click event.

I mean, I don't care if we are nested down say 5 grids?

We can do this:

    <asp:TemplateField HeaderText="Btn Row">
        <ItemTemplate>
            <asp:Button ID="btnRowClick" runat="server" Text="row click" CssClass="btn" 
                OnClick="btnRowClick_Click"/>
        </ItemTemplate>
    </asp:TemplateField>

Note how we just added a OnClick event.

Now in the button click, we can do this:

    protected void btnRowClick_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        GridViewRow gRow = (GridViewRow)btn.Parent.Parent;
        // we now have the grid row
        // row index = gRow.RowIndex
        Debug.Print("first data bound value = " + gRow.Cells[0].ToString());
        // templated controls
        TextBox txtProvince = (TextBox)gRow.FindControl("txtProvince");
        Debug.Print("txt prov = " + txtProvince.Text);

        // if for some reason we want the grid control, then

        GridView Gv = (GridView)GetParentObj(gRow, typeof(GridView));

Now, note how I use .parent. But, sometimes due to a extra div or whatever, it can mess up. So in palce of:

        Button btn = (Button)sender;
        GridViewRow gRow = (GridViewRow)btn.Parent.Parent;

I can use this:

        Button btn = (Button)sender;
        GridViewRow gRow = (GridViewRow)GetParentObj(btn, typeof(GridViewRow));

(it just takes the guess work out of this).

So, I often used to go .Parent.Parent.Parent.Parent to get the gv control, but you don't really neeed to "guess" So, I built this little helper routine:

    public Control GetParentObj(Control c, Type T)
    {
        if (c.Parent.GetType() == T)
            return c.Parent;
        else
            // keep looking
            return GetParentObj(c.Parent, T);
    }

so, we can dump the Grivd view model with this. We can drop in a plane jane asp.net button (don't need a link button). And even a standard button has CommandName and Command arugment (you can even use those outside of a grid - often handy for passing some extra values on a button click.

Now, some don't realize, but how do you write up and add the event for a button? After all, you can't just double click on the button (its buried inside of the nested gv). So you can do this:

type in onclick= --- the instant you type "=", then intel sense will do this:

enter image description here

So, you can then choose create new event. It don't seem like anything occurred, but flip back to code behind - the event stub will be there.

So, when using a GridView, or listview? I just drop in a button - don't care one bit about the gv/lv event model. I drop in a button, create the event stub, and then use the btn.Parent trick to get the lv/gv row item in question.

Also, when a Grid view start to have a LOT of controls? Then i suggest jumping over to a lv, and the reason is that you can drop in plane jane asp.net controls, and you do NOT have to surround them with those "nastly" templatefield tags. For a few, then GridView is ok, but if you start having lots of controls, then a lv becomes a whole lot better and a whole lot less markup