0

I have a GridView. Whenever I'm clicking on the LinkButton after selecting the Dropdown value, I'm getting only the first item of the Dropdown. I need to show the selected value in TextBox. How can i get the desired value ? This is my pseudo code:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" EnableModelValidation="True"
                OnRowDataBound="GridView1_RowDataBound">
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("ID")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Count">
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server" EnableViewState="true" AutoPostBack="false">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Button">
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

And the code behind:

protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("ID", typeof(string));
        dt.Columns.Add("Count", typeof(string));
        dt.Rows.Add("A", "5");
        dt.Rows.Add("B", "8");
        dt.Rows.Add("C", "4");
        dt.Rows.Add("D", "7");
        dt.Rows.Add("E", "9");
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            ddl.Items.Clear();
            int count = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Count").ToString());
            List<string> list = new List<string>();
            for (int i = 1; i <= count; i++)
            {
                list.Add(i.ToString());
            }
            ddl.DataSource = list;
            ddl.DataBind();
        }
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
        DropDownList ddl = (DropDownList)GridView1.Rows[grdrow.RowIndex].FindControl("DropDownList1");
        TextBox1.Text = ddl.SelectedValue.ToString();
    }

Thanks in advance...

Tom
  • 154
  • 2
  • 7
  • 25
user3398663
  • 113
  • 8
  • By 'first item' do you mean the first item in the list you dynamically bind to `ddl` in `GridView1_RowDataBound`? Or is there a default first item you set for the dropdown somewhere in the code? – Dennis R Jul 02 '14 at 22:30
  • No this is the entire code, i've... – user3398663 Jul 03 '14 at 04:35

3 Answers3

0

I'm going to guess that you should implement INotifyProperty change with the databinding. This way every change will be recorded including those made after first change in the drop down box. see ~ http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

umbreon222
  • 247
  • 2
  • 8
0

Since you are dynamically creating and binding the Dropdownlist, it would not have loaded at the point your click event is executed. For you to use bound properties (the selectedValue property in this case), the control must already be loaded and it's viewstate info parsed and set by the control.

The databinding event that creates the dropdownlist runs after pre-render. See http://msdn.microsoft.com/en-us/library/ms178472%28v=vs.85%29.aspx for details on the page lifecycle.

To do what you are asking requires very close attention to the page life cycle. Try moving the code that sets the textbox to the GridView1_RowDataBound method. For this to work though, your control must be rendered with the same properties everytime (that way the event's on it are wired up correctly and the selectedValue property is set correctly).

Another alternative is to handle events on the textbox or dropdownlist itself. You would again have to pay attention to the properties and lifecycle of the control.

ASP.Net Forms uses the SmartUI pattern and IMHO this is the single greatest knock against this pattern.

Louis
  • 1,194
  • 1
  • 10
  • 15
0

Ok, try this

protected void LinkButton1_Click(object sender, EventArgs e)
{
  LinkButton lnkBtn = (LinkButton)sender;
  GridViewRow row = (GridViewRow)lnkBtn.NamingContainer;
  DropDownList ddl= (DropDownList)row.FindControl("DropDownList1");
  TextBox1.Text = ddl.SelectedValue.ToString();
}
Dennis R
  • 3,195
  • 1
  • 19
  • 24