Setting value to hidden field in gridview - asp.net

I have gridview with textbox and one Update button outside gridview.
When i click on button whatever data changed in textbox those values will update in database.
Here is My Gridview templatefield which is having Textbox
<asp:TemplateField HeaderText="SeqNo" HeaderStyle-CssClass="gridViolation-status"
ItemStyle-CssClass="gridViolation-status" ItemStyle-HorizontalAlign="Center"
HeaderStyle-ForeColor="White" SortExpression="STATUS">
<ItemTemplate>
<asp:TextBox ID="txtSeqNo" runat="server" Width="40%" Text='<%# Bind("VIOLATIONCODESEQNO") %>'
onchange="SetValue()"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
and button outside gridview
<asp:Button ID="btnAdd" runat="server" Text="Update"
CssClass="submitbtn" OnClick="btnAdd_Click" />
I just need to update the value that is in textbox to database without using Foreach

Related

ASP.Net GridView DropDownList not posting back with updated data

I have a GridView that contains DropDownLists. They function as expected except during page postback. When the user clicks the update button on the page, I have a sub that loops through the grid rows, performs business ops and saves the data.
The problem is that during postback, the DropDownLists' selected properties do not represent that changes to selections made by the user. The selected item shows 'Dirty = True' in the break point.
Here is a subset of the code I use for reference:
<asp:GridView ID="materialGridView" runat="server"
AutoGenerateColumns="false" >
<Columns>
<asp:BoundField DataField="MaterialTypeName" HeaderText="Material Type" />
<asp:TemplateField>
<HeaderTemplate>
Quantity
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="quantityTextBox" runat="server" Width ="50" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Material
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="materialDropDownList" runat="server" Width="200">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Color
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="colorTextBox" runat="server" Width="100" BackColor="BlanchedAlmond" ReadOnly="true" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
RBK
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButton id="rbkRadioButton" runat="server" Checked="true" />
<asp:TextBox ID="rbkPriceTextBox" runat="server" Width="50" ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Wimsatt
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox ID="wimsattPriceTextBox" runat="server" Width="50"></asp:TextBox>
<asp:RadioButton id="wimsattRadioButton" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
For Each row As GridViewRow In materialGridView.Rows
With row
materialDropDownList = DirectCast(.FindControl("materialDropDownList"), DropDownList)
quantityTextBox = DirectCast(.FindControl("quantityTextBox"), TextBox)
rbkPriceTextBox = DirectCast(.FindControl("rbkPriceTextBox"), TextBox)
wimsattPriceTextBox = DirectCast(.FindControl("wimsattPriceTextBox"), TextBox)
colorTextBox = DirectCast(.FindControl("colorTextBox"), TextBox)
rbkRadioButton = (DirectCast(.FindControl("rbkRadioButton"), RadioButton))
'compare current selecton in drop down and update if nessisary
For Each materialTableRow As DataRow In materialTable.Rows
'the item we are on is the item selected ANDALSO it is not yet assigned to the quote, so it's a new selection, update pricing.
Dim materialTableRowMaterialID As String = bizClass.dbCStr(materialTableRow.Item("MaterialID"))
If materialTableRowMaterialID = materialDropDownList.SelectedValue Then
If IsDBNull(materialTableRow.Item("QuoteID")) Then
rbkPriceTextBox.Text = bizClass.dbCStr(materialTableRow.Item("RBK"))
wimsattPriceTextBox.Text = bizClass.dbCStr(materialTableRow.Item("Wimsatt"))
End If
End If
Next materialTableRow
If rbkRadioButton.Checked = True Then
materialCostSumDecimal += bizClass.strToDec(quantityTextBox.Text) * bizClass.strToDec(rbkPriceTextBox.Text)
chosenSupplierString = "RBK"
Else
materialCostSumDecimal += bizClass.strToDec(quantityTextBox.Text) * bizClass.strToDec(wimsattPriceTextBox.Text)
chosenSupplierString = "Wimsatt"
End If
End With 'row
First of all make sure you are not binding the gridview on postback before reading dropdown values, because databind causes all controls to lose their postback value.
Second gridview is only able to postback the value of the only row that is being edited, so what you need to do is to move the dropdown (and all other controls) to the EditTemplate and set the row mode to Edit so the value will get posted back to server.
But if you want to be able to change all the dropdowns in all rows you might need to use Repeater control instead of GridView.

How to change checkbox value in gridview

I have one gridview. In this gridview one column itemtemplate has check box. I want to change the text of checkbox during check or unchecking the check box.
Can any on help the code.
Thanks In advance
Eshwer
you should be use TemplateField for display your checkbox:
<asp:TemplateField HeaderText="headerText">
<ItemTemplate>
// your image or text for display checked or not
<asp:Label ID="Label6" runat="server" Text='<%# Eval("isView")=="True"?"IS CHECKED":"NOT CHECKED" %>'></asp:Label>
</ItemTemplate>
<EditTemplate>
<asp:CheckBox DataField="isView" runat="server" />
</EditTemplate>
</asp:TemplateField>

How to add checkbox to datagrid in vb.net

I have a datagrid with a set of columns showing data from a database. I create the datatable and add it to the datagrid and then bind the source. this works great and now I would like to add a column to the front of the grid that has checkbox in it.
Do I add the checkbox when I am adding the new row to the datatable that is shown in the datagrid or after I databind the datatable to the datagrid?
Using: VB.Net, Visual Studio 2012
you can add checkbox using template field
Set AutoGenerateColumns attribute to false.
Add Column tag to asp:DataGrid tag.
Now add itemtemplate inside columns
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" type="checkbox" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>
and if you want to attach it to datatable column then u have to add like this
<asp:DataGrid ID="DefaultGrid" Runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkStatus_OnChackedChanged" Checked='<%# Convert.ToBoolean(Eval("Approved")) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:DataGrid>

Validating a TextBox if a Checkbox is checked (inside GridView)

I have the following markup in an ASP.NET page:
<asp:GridView ID="gv" runat="server" DataSourceID="ods" OnRowDataBound="gv_RowDataBound">
<columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="cb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbx" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</columns>
</asp:GridView>
How can I validate the TextBox only if the CheckBox is checked?
I searched for similar situations, but couldn't find anything.
Thanks.
U can use RowCommand under which u can get the control status using int index = AuthorsGridView.EditIndex;
GridViewRow row = AuthorsGridView.Rows[index]; and then validate the text box
refer this http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.aspx

Getting textbox value inside a Detailsview control

I have a DetailsView control with a template field as follows:
<asp:TemplateField SortExpression="Title">
<ItemTemplate>
<asp:TextBox ID="txtMessageTitle" Text='<%# Bind("Title") %>' runat="server">
</asp:TextBox>
<asp:Button ID="btnUpdateTitle" runat="server" Text="Update"
CommandName="UpdateTitle" CommandArgument='<%# Bind("MessageID") %>' oncommand="btnUpdateCommand"/>
</ItemTemplate>
</asp:TemplateField>
The Details View is wrapped inside an UpdatePanel.
When the user clicks on btnUpdateButton I'd like to be able to retrieve the textbox (txtMessageTitle) value in code behind and by using CommandArgument of the button, update the appropriate MessageTitle in database. How can I retrieve the textbox value inside the DetailsView control from the Command event of my button?
Thanks.
use the following:
TextBox txtMessageTitle = detailsViewName.FindControl("txtMessageTitle") as TextBox;
string text = txtMessageTitle.Text;

Resources