Trying to set value inside a textbox in gridview header - c#

I am trying to set the value of a textbox inside the header template of a grid view but not able to do.
<asp:GridView ID="gv" runat="server"
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="ABC">
<HeaderTemplate><asp:TextBox ID="d1" runat="server">ABC</asp:TextBox></HeaderTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.cs side
trying to set value
gv.HeaderRow.FindControl("d1"). ="DEF"
Expected:textbox with Id="d1" should be set as DEF
Actual:Not getting appropriate syntax to bind it after this code gv.HeaderRow.FindControl("d1").

you can do that by adding onRowDataBound event to your grid and write the following code server side
protected void gridView_RowDataBound(object sender,GridViewRowEventArgs e)
{
TextBox txtd1= (TextBox)e.Row.FindControl("d1");
txtd1.Text="your text";
}

You can set Text of textbox after gridview binded. Below code sets text value of textbox
gv.DataSource = list;
gv.DataBind();
((TextBox)gv.HeaderRow.FindControl("d1")).Text = "DEF";

Related

find Countrol inside itemTemplate of Gridview without Gridview Events

i have a Textbox inside ItemTemplate of a Gridview.. i need to find that textbox without using RowDatabound or any other event of Gridview
aspx
<asp:GridView ID="gv_Others" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField >
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<ItemTemplate >
<asp:TextBox ID="txtEmp" runat="server" Width=100% Height=22px CssClass="input"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
aspx.cs
protected void Insert_OtherServices()
{
dsJobCardTableAdapters.Select_OtherServiceTableAdapter dsother = new dsJobCardTableAdapters.Select_OtherServiceTableAdapter();
int count = 0;
foreach (GridViewRow row in gv_Others.Rows)
{
//string b = (row.Cells[0].Controls[0] as DataBoundLiteralControl).Text;
//TextBox a = gv_Others.Rows[count].Cells[0].FindControl("txtEmp") as TextBox;
// string test = a.Text;
//TextBox other = (TextBox)gv_Others.Rows[count].FindControl("txtEmp");
TextBox other = (TextBox)gv_Others.TemplateControl.FindControl("txtEmp");
dsother.Insert_OtherService(other.Text);
count++;
}
}
is there any alternative to get the value from textbox which is inside the item template of Gridview
Do you mean that you want to find text box values without looping through GridView rows? If is is the case, jQuery/javascript can be used to achieve this.
Read all text box values on the client-side. You can find more information on how to do this here
Pass these values along with the postback request using a hidden field. Read and use the hidden field value inside your method.

How to Pass TextBox input to a gridview in asp.net?

I have two textbox and one gridview in my webform. The gridview is binded with database. But I want to add one more column on runtime which will be the textbox input from the webform. Because the scenario is like: I am maintaining two formula to calculate some percentage using the two textbox and the client wants to see this calculation for each row in the gridview.
But I cannot do this.
Is there anyone who can help me on this please? May be some suggestion.
Thanks in advance.
You could add the column in your GridView markup with a label control to display the result as follows.
Here is the markup needed, please note Visible is set to false.
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="label1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use the RowDataBound event to find the label and calculate your result as below:
void GridView1GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//find the control
var label1 = e.Item.FindControl("label1") as Label;
if (label1 != null)
{
if (!string.IsNullOrEmpty(tbInput1.Text) && !string.IsNullOrEmpty(tbInput2.Text))
{
// Do the calculation and set the label
label1.Text = tbInput1.Text + tbInput2.Text;
// Make the column visible
GridView1.Columns[0].Visible = true;
}
}
}
}
Please forgive any errors, I have not tested the above.

dynamically add a link button in gridview template field at run time together a label control

I have a gridview in my form. I am working with RowDataBound event to the grid. Now I have a template field inside columns of the gridview. A label has been taken inside template field. I want to add a link to this label on RowDataBound event at runtime, but .System.Web.UI.WebControls.LinkButton is showing instead of link button.
How do I add a link button with label text in the grid view?
Just add a linkbutton inside your templatefield
<asp:GridView runat="server" ID="gridView">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkTest"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then in your rowdatabound event you can find it and do whatever you want
void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Entity entity = e.Row.DataItem as Entity;
LinkButton lnkTest = e.Row.FindControl("lnkTest") as LinkButton;
lnkTest.CommandArgument = entity.ID.ToString();
lnkTest.Text = entity.Name;
}
}
You can then subscribe to gridview Command event and correct CommandArgument will be passed when clicking by linkbutton.
Hope this helps.

How to add dynamic Texbox in GridView at RunTime?

I would like to add controls like textBox in GridView from code on the fly.
In my project i have one Grid in that i can't decide how many rows and columns are there. so that i just give it DataSource. This working fine.
GridView G = new GridView();
G.DataSourse = dt;
G.DataBind();
now i want to do such thing that in Gridview all the controls are Textbox control so that i can write in that textbox.
TextBox t= new TextBox();
G.Contorls.Add(t);
This will throw exception...
do anyone have any idea about this???
Thanks in advance..
Regards
Amit Vyas
Why not do it in design time with ItemTemplate
<asp:GridView ID="GrdDynamic" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="Name" Text='<%#Eval("Name") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
EDIT
Here is an interesting CodeProject post on dynamically adding template columns
Please check this http://www.devasp.net/net/articles/display/708.html link and use below code:
DropDownList ddl = new DropDownList();
ddl.Visible = true;
ddl.ID = "ddl1";
ddl.Items.Add("Item1");
TableCell cell = new TableCell();
gv.Rows[0].Cells.Add(cell);
gv.Rows[0].Cells[0].Controls.Add(ddl);
If you are looking a way to add TextBox dynamically in the existing GridView, then using RowDataBound event of the GridView would be best solution.
Add a PlaceHolder control in the ItemTemplate field.
<asp:GridView ID="GrdDynamic" runat="server" OnRowDataBound="GridView_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:PlaceHolder runat='server' ID="PlaceHolder1"></asp:PlaceHolder>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
//find placeholder control
PlaceHolder placeHolder = e.Row.FindControl("PlaceHolder1") as PlaceHolder;
TextBox TextBox1 = new TextBox();
placeHolder.Controls.Add(TextBox1);
}

How to set a column in gridview as hyperlink which is autogenerated

I want to make the gridview.columns[0] as hyperlink. I tried so many work around mentioned in different sites. I am binding a list<> to the grid. and I need to make the first column as hyperlink and upon clicking that link, it should be redirected to a page with the corresponding item.
Which event I need to use and how can I pass that value from the list?
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = firstCell.Text, Text = firstCell.Text });
}
}
Be warned that if you bind data to grid only first time page loaded then your changes will disappear.
You have to make that column as Template Column
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text="test" NavigateUrl='<%# Eval("fieldName", "show.aspx?ID={0}") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>

Categories