hi, your current approach won't work because you are creating dynamic controls in the RowDataBound event handler. This handler fires only when databinding and normally you do not want to databind on postback. This handler won't fire and your dynamic controls
do not get recreated.
To get your code working, you need to create your dynamic controls in the RowCreated event handler of your GridView. This handler will fire even on postback and is the appropriate place. Ofcourse now you have a new problem. Your logic depends on the DataItem.
The DataItem is still not available in this phase. Typical chicken or the egg dilema!
To workaround, I suggest that you add your control declaratively in a panel control and play with the visibility in RowDataBound handler based on your current logic.
Thanks Alessandro. I agree with your solution to play with visibility. My problem is when Ihave to retrieve the value of this control on the rowcommand event, I think, I have to use the cells(x).findcontrol to retrieve value.
In RowDataBound I set the visibility of textbox or listbox looking the value of dataitem, right?
In the rowcommands how could I know if I can get the value from the listbox or fron the testbox ?
Antonio, yes in RowDataBound you can set the visibility.
As for retrieving your control, you can use your ButtonField's CommandArgument value. By default, the row index is stored in the button fields CommandArgument which is exposed in the RowCommand. Eg :
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "btn_insert":
int rowIndex;
if (int.TryParse(e.CommandArgument.ToString(), out rowIndex))
{
TextBox tb = (TextBox)GridView1.Rows[rowIndex].FindControl("txt_rispo");
}
break;
}
}
hi, that's actually quite simple. You may try either of the following.
int rowIndex;
if (int.TryParse(e.CommandArgument.ToString(), out rowIndex))
{
Control c = GridView1.Rows[rowIndex].FindControl("Control-Id");
if (c is TextBox)
{
Response.Write(((TextBox)c).Text);
}
else if (c is ListBox)
{
//cast to listbox and do something else here
}
}
that's in case you cannot determine the control type. If however you mean you know the control type but don't know if that control will exist, then in that case you can use the "as" keyword and test for null like this :
int rowIndex;
if (int.TryParse(e.CommandArgument.ToString(), out rowIndex))
{
//This won't throw an error if the TextBox is not found
//If it's not found, it will simply be null
TextBox tb = GridView1.Rows[rowIndex].FindControl("txt_rispo") as TextBox;
ListBox lb = GridView1.Rows[rowIndex].FindControl("lb_rispo") as ListBox;
//So check for null first :
if (tb != null)
{
// lets do something with the textbox
}
if (lb != null)
{
//lets do something with the listbox
}
}
azz, I'm not sure why I mentioned the "as" keyword. It actually brings nothing to the table in your use case scenario. Basically this keyword won't throw an error if conversion fails (if you tried casting to the wrong control).
TextBox tb = (TextBox)GridView1.Rows[rowIndex].FindControl("txt_rispo");
if (tb != null)
{
// lets do something with the textbox
}
The above is just fine. The key here is to test for null.
rod72
Member
2 Points
5 Posts
Add textbox ro a Gridview and getting value on "RowCommand" event
May 16, 2009 03:47 PM|LINK
Hi at all!
this my gridview:
<
asp:GridView ID="Gd_dom" runat="server" AutoGenerateColumns="False" BackColor="#CA9560" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Font-Names="Arial"> <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> <Columns> <asp:BoundField DataField="Settimana" HeaderText="Settimana"/> <asp:BoundField DataField="Anno" HeaderText="Anno" /> <asp:BoundField DataField="Arg_code" HeaderText="Codice Argomento" /> <asp:TemplateField HeaderText="Risposta"> <ItemTemplate> </ItemTemplate> </asp:TemplateField> <asp:ButtonField ButtonType="Link" CommandName="btn_insert" HeaderText="Inserimento" Text="Inserisci" /> </Columns> <RowStyle BackColor="White" ForeColor="#330099" /> <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> </asp:GridView>On the "RowDataBound" event I add a new textbox or a new listbox (it depends on the dataitem"tendina" value) as follow:
If
e.Row.DataItem("tendina") = "N" Then ktextBox = New WebControls.TextBoxktextBox.ID =
"txt_rispo"ktextBox.BackColor = Drawing.Color.Beige
ktextBox.Width = 200
e.Row.Cells(3).Controls.Add(ktextBox)
e.Row.Cells(3).Controls(0).EnableViewState =
True------------------------------------------------------------------------
If I check if the new control is created with this command:Response.Write("controlli: " & e.Row.Cells(3).FindControl("txt_rispo").ToString)
the answer is : True
When I try to get the value of the new control (textbox or listbox) on the "rowcommand" event I have an error ...
It seem that the new control added is not recognized. If I check with this command:
Response.Write("controlli: " & Gd_dom.Rows(irow).Cells(3).FindControl("txt_rispo").ToString)
The answer is False.
How could I get the value of the new control on the "rowcommand" event ?
Thanks,
Antonio
GridView FindControl Child Template Field ASP.NET 2.0 GridView SelectedValue grid view columns .NET Gridview Problems grid view row Grid view Cells gridview insert GridView findcontrol row GridViewRow grid view cell gridview empty cell value GridView dynamic column RowCommand ASP.NET 2.0/GRIDVIEW asp.net link button sql template gridview commandname oncommand search accessing controls asp.NET GridView GridView databound grid view row ASP .net.NET Grid View Grid View Update ASP .net gridview grdiview commands ASP.NET 2.0 VB.NET gridvew dynamic data bind gridview control Grid view Text Box Template Field GridView Table TextBox Grid View Postback Grid view Onclick click row value
Pushkar
Contributor
5747 Points
1111 Posts
Re: Add textbox ro a Gridview and getting value on "RowCommand" event
May 16, 2009 04:18 PM|LINK
HI
Here link what you are looking for.
====================================
Pushkar
alessandro
Contributor
6800 Points
1105 Posts
Re: Add textbox ro a Gridview and getting value on "RowCommand" event
May 16, 2009 04:27 PM|LINK
hi, your current approach won't work because you are creating dynamic controls in the RowDataBound event handler. This handler fires only when databinding and normally you do not want to databind on postback. This handler won't fire and your dynamic controls do not get recreated.
To get your code working, you need to create your dynamic controls in the RowCreated event handler of your GridView. This handler will fire even on postback and is the appropriate place. Ofcourse now you have a new problem. Your logic depends on the DataItem. The DataItem is still not available in this phase. Typical chicken or the egg dilema!
To workaround, I suggest that you add your control declaratively in a panel control and play with the visibility in RowDataBound handler based on your current logic.
rod72
Member
2 Points
5 Posts
Re: Add textbox ro a Gridview and getting value on "RowCommand" event
May 16, 2009 09:19 PM|LINK
Thanks Alessandro. I agree with your solution to play with visibility. My problem is when Ihave to retrieve the value of this control on the rowcommand event, I think, I have to use the cells(x).findcontrol to retrieve value.
In RowDataBound I set the visibility of textbox or listbox looking the value of dataitem, right?
In the rowcommands how could I know if I can get the value from the listbox or fron the testbox ?
alessandro
Contributor
6800 Points
1105 Posts
Re: Add textbox ro a Gridview and getting value on "RowCommand" event
May 16, 2009 09:36 PM|LINK
Antonio, yes in RowDataBound you can set the visibility.
As for retrieving your control, you can use your ButtonField's CommandArgument value. By default, the row index is stored in the button fields CommandArgument which is exposed in the RowCommand. Eg :
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName.ToLower())
{
case "btn_insert":
int rowIndex;
if (int.TryParse(e.CommandArgument.ToString(), out rowIndex))
{
TextBox tb = (TextBox)GridView1.Rows[rowIndex].FindControl("txt_rispo");
}
break;
}
}
rod72
Member
2 Points
5 Posts
Re: Add textbox ro a Gridview and getting value on "RowCommand" event
May 19, 2009 07:13 AM|LINK
Yes, you're right but it's not sure that in the row ia have the textbox. I may have a Listbox (look at my rowdatabound event).
So may I have to use:
TextBox tb = (TextBox)GridView1.Rows[rowIndex].FindControl("txt_rispo");
or
ListtBox lb = (ListBox)GridView1.Rows[rowIndex].FindControl("lb_rispo");
How could I do ?
alessandro
Contributor
6800 Points
1105 Posts
Re: Add textbox ro a Gridview and getting value on "RowCommand" event
May 19, 2009 10:45 AM|LINK
hi, that's actually quite simple. You may try either of the following.
int rowIndex;
if (int.TryParse(e.CommandArgument.ToString(), out rowIndex))
{
Control c = GridView1.Rows[rowIndex].FindControl("Control-Id");
if (c is TextBox)
{
Response.Write(((TextBox)c).Text);
}
else if (c is ListBox)
{
//cast to listbox and do something else here
}
}
that's in case you cannot determine the control type. If however you mean you know the control type but don't know if that control will exist, then in that case you can use the "as" keyword and test for null like this :
int rowIndex;
if (int.TryParse(e.CommandArgument.ToString(), out rowIndex))
{
//This won't throw an error if the TextBox is not found
//If it's not found, it will simply be null
TextBox tb = GridView1.Rows[rowIndex].FindControl("txt_rispo") as TextBox;
ListBox lb = GridView1.Rows[rowIndex].FindControl("lb_rispo") as ListBox;
//So check for null first :
if (tb != null)
{
// lets do something with the textbox
}
if (lb != null)
{
//lets do something with the listbox
}
}
alessandro
Contributor
6800 Points
1105 Posts
Re: Add textbox ro a Gridview and getting value on "RowCommand" event
May 19, 2009 10:58 AM|LINK
azz, I'm not sure why I mentioned the "as" keyword. It actually brings nothing to the table in your use case scenario. Basically this keyword won't throw an error if conversion fails (if you tried casting to the wrong control).
TextBox tb = (TextBox)GridView1.Rows[rowIndex].FindControl("txt_rispo");
if (tb != null)
{
// lets do something with the textbox
}
The above is just fine. The key here is to test for null.