How can I read a dynamically created textbox (gridview OnRowUpdating) - asp.net

<asp:GridView ID="GridView1" runat="server" >
<asp:TemplateField HeaderText="Token" SortExpression="Token" HeaderStyle-Width="100px">
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
update:
after i view the source code of the page thsi is what i see the id of a textbox that i have created dynamic.
ctl00_ContentPlaceHolder1_tabControl_tabUsers_MyControl1_gv_ctl02__token0_3
OnRowUpdating:
TextBox _token = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_token " + e.RowIndex + "_" + rowId) as TextBox;
Update end:
i am adding few textbox dynamic in OnRowDataBound and whe i try getting the value then i am getting null
here is my code:
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
for (int rowId = 0; rowId < 5; rowId++)
{
TextBox _token = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_token" + rowId) as TextBox;
}
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int rowId = 0; rowId < 5; rowId++)
{
TextBox txtBox = new TextBox();
txtBox.ID = "_token" + rowId;
txtBox.Text = "token" + rowId;
e.Row.Cells[7].Controls.Add(txtBox);
}
}

here is how i able to fix the problem: instead of creating in rowdatabound i am creating on RowCreated, hope this will help others.
protected void gridviwe1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int rowId = 0; rowId < 5; rowId++)
{
TextBox txtBox = new TextBox();
txtBox.ID = "_registration" + e.Row.RowIndex + "_" + rowId;
txtBox.Text = "_registration" + e.Row.RowIndex + "_" + rowId;
e.Row.Cells[7].Controls.Add(txtBox);
}
}
}

You are creating textbox for each row - 5 of them... and in each row, each of those textboxes has the same id as the other rows. You need to at the row's index, for example, to the name of the textboxes when you create them. You can't have a control on the page with the same id, or else it can't be found correctly.
Here is one way to do that.
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int rowId = 0; rowId < 5; rowId++)
{
TextBox txtBox = new TextBox();
txtBox.ID = "_token" + e.Row.RowIndex + "_" + rowId;
txtBox.Text = "token" + rowId;
e.Row.Cells[7].Controls.Add(txtBox);
}
}
I can't test that this is the complete solution, but it is a place to start.

Related

How to access dynmaically added Textbox values ASP.Net

I have a gridview placed on a ASP.Net WebSite, and added a column dynamically like this:
protected void gvGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (DataControlRowType.DataRow == e.Row.RowType && e.Row.RowState != DataControlRowState.Edit &&
(e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
TextBox tb = new TextBox();
tb.ID = "tb";
tb.Attributes.Add("runat", "server");
int i = e.Row.Cells.Count;
i = i - 1;
e.Row.Cells[i].Controls.Add(tb);
}
}
The gridview is populated, and at the end of every row the TextBox is added. When I look at the HTML Code that is delived to the Browser, I can see that the ID of every Textbox is "gv_Items_tb_X" with X being the current row index.
Now I would like to access the content that the user types in the tb on a button click. I tried following code, but it I get an exception becuase the textbox is null.
protected void btn_UpdateCount_Click(object sender, EventArgs e)
{
OI_Data_Service.OI_Data_ServiceClient sc = new OI_Data_Service.OI_Data_ServiceClient();
foreach (GridViewRow row in gv_Items.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
TextBox tb_value = (TextBox)gv_Items.Rows[row.RowIndex].Cells[5].FindControl("gv_Items_tb_" + row.RowIndex);
sc.UpdateItemOnOpening("1", row.Cells[0].Text, tb_value.Text);
}
}
Response.Redirect("~/okay.aspx");
}
Can anyone tell my what I am doing wrong?
Thanks!

getting column header text inside rowdatabound event

I need to get column header inside the DataControlRowType.Data of rowdatabound event of GridView and here's how I'm doing it:
((DataTable)((GridView)sender).DataSource).Columns[i].ColumnName
Is there another, possibly more concise than shown above? Just wanted to check out here.
if (e.Row.RowType == DataControlRowType.Header) {
LinkButton LnkHeaderText = e.Row.Cells[1].Controls[0] as LinkButton;
LnkHeaderText.Text = "Name";
}
protected void gvLista_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
for (int i = 0; i < gvLista.HeaderRow.Cells.Count; i++)
{
string cabecera = gvLista.HeaderRow.Cells[i].Text;
if (cabecera.Equals("ofertaactiva"))
{
int activo = int.Parse(e.Row.Cells[i].Text);
if (activo != 1)
{
e.Row.BackColor = System.Drawing.Color.SandyBrown;
}
break;
}
}
}
}
Here is a way
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Text = "Date";
}
}
But the question is why some one would like to change the header name at run-time.
You can use bound-field with header name as follows (need auto generate column to false)
<Columns>
<asp:BoundField DataField="DateField" HeaderText="Date"
SortExpression="DateField" />
</Columns>
Edit-1
To get the column name change it
var columnName= e.Row.Cells[0].Text ;
At least in VB.NET 2010 this approach is flawed -- for some reason (PRB?) e.Row.Cells(x).Text = "" in the RowDataBound event.
Private Sub gvSymbolList_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvSymbolList.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
Dim i As Integer
For i = 0 To e.Row.Cells.Count - 1
Select Case e.Row.Cells(i).Text.ToLower
Case "symbol"
COL_SYMBOL = i
Case "description"
COL_DESCRIPTION = i
Case "last"
COL_CLOSE = i
Case "change"
COL_CHANGE = i
End Select
Next
End If

Converting Gridview to RadGrid

How do I change the below code from GridView to RadGrid? Below is the code for my gridview:
protected void gv_Movie_RowCommand(object sender, GridViewCommandEventArgs e)
{
//get the row number of the selected row
int rowNo = int.Parse(e.CommandArgument.ToString());
//get the selected row
GridViewRow row = gv_Movie.Rows[rowNo];
//Get movie ID, which is on the 1st column of the gridview
string movieID = row.Cells[0].Text;
if (e.CommandName == "Select")
{
Response.Redirect("MovieSelect.aspx?id=" + movieID);
}
else if (e.CommandName == "Update")
{
Response.Redirect("MovieUpdate.aspx?id=" + movieID);
}
}
I tried the below code and it doesn't work at all due to e.CommandArgument. Any solution to this?
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
int rowNo = int.Parse(e.CommandArgument.ToString());
GridDataItem row = RadGrid1.Items[rowNo];
string movieID = row.Cells[0].Text;
if (e.CommandName == "Select")
{
Response.Redirect("movieSelect.aspx?id=" + movieID);
}
else if (e.CommandName == "Delete")
{
Response.Redirect("movieUpdate.aspx?id=" + movieID);
}
}
instead of
int rowNo = int.Parse(e.CommandArgument.ToString());
GridDataItem row = RadGrid1.Items[rowNo];
use
GridDataItem row = e.Item as GridDataItem;
Telerik automatically retrieves current row as e.Item. The rest should work the same.
Or better yet, if appropriate, try to Utilize DataKeyNames.
So in your markup, you will have something like:
<telerik:RadGrid id="grid" runat="server">
<MasterTableView DataKeyNames="movieID">
.....
</MasterTableView>
</telerik:RadGrid>
Then, you can retireve movieID like this:
var row = e.Item as GridDataItem;
string movieID = row.GetDataKeyValue("movieID");

Gridview dynamic controls problem

I have Gridview set to autogenerate columns=true i have created some dynamic text boxes for some columna nd footer now ehn i click on footer button gridview rowcomand event is not fired, to fire this command i have to bind the gridview again but when i bind my values which i have changes in textboxes gone..
following is my code rowdatabound event
protected void grdMaterialPercentage_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (grdMaterialPercentage.AutoGenerateColumns == true)
{
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Visible = false;
if (DataBinder.Eval(e.Row.DataItem, "AgentName").ToString() != string.Empty)
{
int i = 0;
foreach (TableCell c in e.Row.Cells)
{
if (i >= 3)
{
TextBox tb = new TextBox();
tb.Text = c.Text;
tb.ID = "txtbox" + i.ToString();
tb.Style.Add("Width", "25px");
tb.Style.Add("Height", "15px");
c.Controls.Clear();
c.Controls.Add(tb);
}
i++;
}
}
else
{
e.Row.Visible = false;
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[0].Visible = false;
int j = 0;
foreach (TableCell c in e.Row.Cells)
{
if (j >= 3)
{
DataRow dr = dt.Rows[dt.Rows.Count - 1];
LinkButton btn = new LinkButton();
btn.ID = "FooterButton" + j.ToString();
btn.CommandName = j.ToString();
btn.Text = "Save" + dr[j - 1].ToString();
btn.CssClass = "button";
btn.Style.Add("align", "center");
btn.CommandArgument = dr[j - 1].ToString();
btn.OnClientClick = "return ValidateTotalPercentage('" + j + "')";
c.Controls.Clear();
c.Controls.Add(btn);
} j++;
}
}
}
}
I suggest you use a tool such as Firebug to see what is happening under the hood with your Javascript when you click the link on the grid. This is what I would do in your shoes.

how to retain viewstate for the dynamic controls created in GRIDVIEW

i am creating dynamic textbox onRowCreated event in gridview control, however when i try to findcontrol i get null
here is how i am dong...
protected void gvORg_RowCreated(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
txBox txtReg = new TextBox();
txtReg.ID = "_registration" + e.Row.RowIndex + rowId.ToString();
txtReg.Text = reg.RegistrationToken;
e.Row.Cells[7].Controls.Add(txtReg);
}
}
}
protected void gvOrg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
.....
....
TextBox _registration1 = gvOrg.Rows[e.RowIndex].Cells[7].FindControl("_registration" + e.RowIndex + rowId) as TextBox;
}
Have you tried to find it by:
GridView gv = (GridView)sender;
GridViewRow gvr = (GridViewRow)gv.Rows[e.RowIndex];
(TextBox)gvr.FindControl("_registration" + e.Row.RowIndex + "_" + reg.RegistrationId.ToString())
i able to fix my problem here

Resources