How to set boundfield value to template field control? - c#

I have gridview output like this .
when I click edit link, I need to pass unitsinstock value should transfer to textbox control. which is present in templatefield.
Here is my c# code:
TextBox tt = (TextBox)GridView1.Rows[i].Cells[3].FindControl("TextBox2").ToString();
TextBox text_ref = (TextBox)GridView1.Rows[e.NewEditIndex].Cells[2].FindControl("TextBox2");
TextBox3.Text = text_ref.Text;
Is there anything wrong? when I debugging e.NewEdtIndex=0, TextBox3=null. How to solve this?

You should use EditItemTemplate for this. For exemple, this code above will bound the unitsinstock value to unitsinstock copy text box when you edit the row.
Change The SqlDataSource as well:
<UpdateParameters>
<asp:Parameter Name="unitsinstock" Type="Int32" /> /*Put the correcly type here*/
/*other fields*/
</UpdateParameters>
And the GridView:
<asp:TemplateField HeaderText="unitsinstock">
<ItemTemplate>
<asp:label id="labelUnitsinstock" runat="server" text='<%#Eval("unitsinstockActual")%>'>
</asp:label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="unitsinstock copy">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Bind("unitsinstock") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
Take a look here for more information Using TemplateFields in the GridView Control

Related

How to select a cell in datagrid onClick in ASP.net C#

I am importing a column in datatable to my grid. Now I want navigate to a new page on selecting a cell in grid by fetching the selected value. I have tried this by including bound field in my grid like
<asp:GridView ID="GDV_1" runat="server" EnableModelValidation="true" AutoGenerateColumns="false" OnSelectedIndexChanged="GDV_1_SelectedIndexChanged" AutoGenerateSelectButton="false" DataKeyNames="SROID">
<Columns>
<asp:BoundField DataField="SRONameinEnglish" HtmlEncode="False" DataFormatString="<a target='_blank' href='Test.aspx?code={0}>ClickHere</a>" />
</Columns>
</asp:GridView>
Doing this way my requirement is achieved but the all cells are displaying Common text Click here instead of showing data from Database.
Please give your suggestion on how to get the value from database into cell and make it clickable. I don't want to use Select button. Please find my current output.
This is my current output I want my data from DB instead of ClickHere.
You can use TemplateField
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnk<%# Eval("SRONameinEnglish")%>"><%# Eval("SRONameinEnglish")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
and click of LinkButton put your code to navigate anywhere.
In your case you are binding boundfield with static a tag which have href attribute so your not able to change text on that boundfield from your database.To get your approach you should
use TemplateField and bind data with text attribute using eval keyword as below example.
Try it:
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
OR
you can also bind link with your hyperlink using NavigateUrl property of hyperlink as below example.
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:HyperLink id="HyperLink2" NavigateUrl='<%#Eval("YourUrl") %>' Text='<%#Eval("name") %>' runat="server"/>
</ItemTemplate>
</asp:TemplateField>
I hope it will helpful to you.

How to set font-size of text box in bounded fields of grid view template fields?

In ASP.NET 2.0 web application, there is a gridview and user wanted to change the font size of contents of that grid view. Below is Gridview definition and server side code to set the font-size of contents of gridview. Everything is fine except, textboxes in bounded fields of gridview. The font size does not applied on them.
GridView :
<asp:TemplateField HeaderText="Display Name" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Width="100px" Text='<%# Bind("DisplayName") %>' OnTextChanged="TextBox_TextChanged" />
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("DisplayName") %>' />
</ItemTemplate>
</asp:TemplateField>
ServerSide Code:
ObjPListSetting.Style["font-size"] = sTextSize + "px";
where, sTextSize is target value (i.e. 12, 14, 16).
Why is that so? Anyone can help in this regard.
In the OnRowDataBound event hander do something like this.
TextBox txtTextBox1 = RowObject.FindControl("TextBox1");
txtTextBox1.Style["font-size"] = sTextSize + "px";
This is a dummy code. Just check how you get the rowobject in below link.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

asp.net dynamically created gridview edit template field

I have a gridview to which I dynamically bind data to.
It features SELECT and EDIT commands.
When it is in EDIT mode all the fields turn into textboxes that can be edited.
What I would like to do is add an Ajax Calendar Extender to one of the textboxes.
How can I create a TemplateField just for the one field?
Is it even possible? I know how to do it if I bound all fields and assign the data via a SqlDataSource.
I have tried adding the templatefield to my gridview but the data is just not showing up:
<asp:GridView ID="gvCheckResults" runat="server" OnRowDataBound="gvCheckResults_RowDataBound"
OnRowEditing="gvCheckResults_RowEditing" OnRowUpdating="gvCheckResults_RowUpdating" OnRowCancelingEdit="gvCheckResults_RowCancelingEdit"
OnRowDeleting="gvCheckResults_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="DateOfTest" SortExpression="DateOfTest">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="test" runat="server"
TargetControlID="TextBox1"
CssClass="calendar"
Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How the data gets bound:
protected void TestDataBind()
{
string Name = txtCheckName.Text;
if (string.IsNullOrEmpty(Name))
Name = null;
gvCheckResults.DataSource = dataContext.GetRecords(Name);
gvCheckResults.DataBind();
}

GridView row update and DropList

I have a gridview with column that have droplist when enter edit mode:
<asp:TemplateField HeaderText="genre" SortExpression="genre">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("genre") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now i want to take it to UpdateParameters of SqlDataSource:
<UpdateParameters>
<asp:ControlParameter ControlID="DropDownList2" Type="string" PropertyName="SelectedValue" Name="genre" />
</UpdateParameters>
But when i pressed he give me Error msg
Could not find control 'DropDownList2' in ControlParameter 'genre'.
Any idea why?
DropDownList2 is a nested control located under the Grid; therefore, your SqlDataSource control does not have visibility at all of the DropDownList2.
You could try assigning the value on the code behind by using the Updating event:
protected void SqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#genre"].Value = GetDropDownListValue();
}
Note: You will need to use FindControl("DropDownList2") in GetDropDownListValue()

Get gridview values from code behind

I want to get the value of a hidden field in a grid view from code-behind, but not to be used in the _RowDataBound or any other similar method. Here is my present code (it is a shopping cart scenario):
<asp:GridView ID="gvShoppingCart"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
DataKeyNames="ID"
ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="lblProductID" runat="server" Text='<%# Eval("ProductID") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("ProductID", "product_details.aspx?id={0}") %>'
Text='<%# GetProduct(Eval("ProductID")) %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" runat="server" Width="35" CssClass="input" onkeypress="return isNumberKey(event)" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
For the sake of brevity I removed certain fields since they are there only for the display. The Quantity field is there for the user to input a number to add a number of products to his cart. I wish to access the lblProductID label in the _TextChanged event. In this same event, I tried
Label lblProductID = (Label)gvShoppingCart.FindControl("lblProductID");
but it didn't work and returns only a null value. What is the solution?
For each row in your GridView there is a HiddenField for the ProductID.
You can access the HiddenField of a row (in the example below the first row) by using the following code (assuming your HiddenField is in the first cell):
HiddenField hiddenFieldProductID =
(HiddenField)gvShoppingCart.Rows[0].Cells[0].FindControl("lblProductID");
string productID = hiddenFieldProductID.Value
// Do something with the value
Hope, this helps.
Try to replace the HiddenField to a label or a textbox and set the visible attribute to false.
I had tried this before and it works.

Categories