How to add new row to a databound gridview? - asp.net

I'm trying to add a new blank row to my gridview whenever the user clicks the Add row button. Ideally, whichever row they click this button, a new row would be inserted just below that row.
I've read through a lot of the previous questions but I still cannot get my gridview to display a new blank row.
Page code:
<table>
...
...
<td colspan="3" align="left" style="border: thin solid #000000; vertical-align: top; background-color: #4B6C9E">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:GridView ID="grdHours" runat="server" AutoGenerateColumns="False" Width="100%" CellPadding="4"
ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" VerticalAlign="Top" Height="20px"/>
<Columns>
<asp:TemplateField ShowHeader="False" HeaderText="Task">
<ItemTemplate>
<asp:Label ID="BufferLabel" runat="server" Text="" Visible="False"></asp:Label>
<asp:TextBox ID="TaskList" runat="server" CssClass="myDropDownSearch" Width="80%"
onfocus="inputFocus(this)" onblur="inputBlur(this)" value="Select a Task..."></asp:TextBox>
<asp:AutoCompleteExtender ServiceMethod="SearchEmployees"
MinimumPrefixLength="3"
CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
TargetControlID="TaskList"
ID="AutoCompleteExtender3" runat="server" FirstRowSelected = "false">
</asp:AutoCompleteExtender>
</ItemTemplate>
<ItemStyle Width="15%" />
</asp:TemplateField>
<asp:BoundField DataField="Weekday" HeaderText="Weekday" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="4%" />
</asp:BoundField>
<asp:BoundField DataField="Date" DataFormatString="{0:d}" HeaderText="Date" >
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" Width="8%" />
</asp:BoundField>
<asp:TemplateField HeaderText="Hours">
<ItemTemplate>
<asp:Label ID="lblHours" Visible='<%# NOT IsInEditMode %>' runat="server" Text='<%# Eval("Hours") %>' CssClass="hoursLabel" />
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Hours") %>'
Width="90%" Visible='<%# IsInEditMode %>'>
</asp:TextBox>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle Width="5%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Minutes">
<ItemTemplate>
<asp:Label ID="lblMins" Visible='<%# NOT IsInEditMode %>' runat="server" Text='<%# Eval("Minutes") %>' CssClass="hoursLabel" />
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="myDropDown" selectedValue='<%# Eval("Minutes") %>' Visible='<%# IsInEditMode %>' >
<asp:ListItem></asp:ListItem>
<asp:ListItem>0</asp:ListItem>
<asp:ListItem>15</asp:ListItem>
<asp:ListItem>30</asp:ListItem>
<asp:ListItem>45</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle Width="5%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Activity" >
<ItemTemplate>
<asp:Label ID="lblActivity" Visible='<%# NOT IsInEditMode %>' runat="server" Text='<%# Eval("Activity") %>' CssClass="hoursLabel" />
<asp:DropDownList ID="DropDownList1" runat="server" width="75%" selectedValue='<%# Eval("Activity") %>' Visible='<%# IsInEditMode %>'>
<asp:ListItem Selected="True">Select Activity</asp:ListItem>
<asp:ListItem></asp:ListItem>
<asp:ListItem>Prescope</asp:ListItem>
<asp:ListItem>Scope</asp:ListItem>
<asp:ListItem>Design/Build Configuration</asp:ListItem>
<asp:ListItem>Testing</asp:ListItem>
<asp:ListItem>Training</asp:ListItem>
<asp:ListItem>Implementation/Validation</asp:ListItem>
<asp:ListItem>Maintenance & Ongoing Support</asp:ListItem>
<asp:ListItem>Project Management</asp:ListItem>
<asp:ListItem>Shared Time</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" visible="false" ControlToValidate="DropDownList1"></asp:RequiredFieldValidator>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle Width="20%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="AddEntries">
<ItemTemplate>
<asp:Button ID="btnHoursRow" runat="server" Text="+" Visible='<%# IsInEditMode %>' OnClick="btnNewEntryRow_Click" />
</ItemTemplate>
<ItemStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="AddNotes">
<ItemTemplate>
<asp:Label ID="lblNotes" Visible='<%# NOT IsInEditMode %>' runat="server" CssClass="hoursLabel" />
<asp:Button ID="btnAddNotes" runat="server" Text="Add Notes" Visible='<%# IsInEditMode %>' />
<asp:ModalPopupExtender ID="btnAddNotes_ModalPopupExtender" runat="server"
DynamicServicePath="" Enabled="True" PopupControlID="pnlNotes"
TargetControlID="btnAddNotes"
OkControlID="btnNotesDone">
</asp:ModalPopupExtender>
</ItemTemplate>
<ItemStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="Validator">
<ItemTemplate>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Whole Numbers Only" ControlToValidate="TextBox1" ValidationGroup="HoursasIntegers"
ValidationExpression="\d+" SetFocusOnError="False" Width="75%" Font-Bold="True" ForeColor="Red" Display="Dynamic"></asp:RegularExpressionValidator>
</ItemTemplate>
<ItemStyle Width="10%" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False" HeaderText="ErrMessage">
<ItemTemplate>
<asp:Label ID="ActivityErrorMessage" runat="server" Text="You must select an Activity for entered hours." Visible="False" ForeColor="#FF0000" Font-Bold="True"></asp:Label>
</ItemTemplate>
<ItemStyle Width="19%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="TimeNotes">
<ItemTemplate>
<asp:Panel ID="pnlNotes" runat="server" Visible='<%# IsInEditMode %>' Width="75%" BackColor="#4B6C9E" Height="300px">
<table width="100%">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Enter Notes Below:" ForeColor="White" Font-Size="Medium"></asp:Label>
</td>
</tr>
<tr>
<td align="center">
<asp:TextBox ID="txtNotes" runat="server" CssClass="myTextbox"
TextMode="MultiLine" Text='<%# Eval("TimeNotes") %>' Height="200"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnNotesDone" runat="server" Text="Done" />
<asp:Button ID="btnNotesCancel" runat="server" Text="Cancel" OnClick="btnNotesCancel_Click" Visible="False" />
</td>
</tr>
</table>
</asp:Panel>
</ItemTemplate>
<ItemStyle Width="4%" />
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="White" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" Height="20px" VerticalAlign="Top" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</td>
Code behind:
Protected Sub btnNewEntryRow_Click(ByVal sender As Object, ByVal e As EventArgs)
' Dim clickedRow = TryCast(DirectCast(sender, Button).NamingContainer, GridViewRow)
' Dim clickedIndex = clickedRow.RowIndex
'Just hard coded a value for now
BindGrdHours(3)
End Sub
Private Sub BindGrdHours(ByVal rowIndex As Integer)
Dim rowIndex As Integer = 0
If ViewState("CurrentGridTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentGridTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim TaskList As TextBox = DirectCast(grdHours.Rows(rowIndex).Cells(1).FindControl("TaskList"), TextBox)
'Dim TextBoxAge As BoundField
'TextBoxAge.DataField = grdHours.Rows(rowcount).Cells(2).FindControl("Weekday").ToString
'Dim TextBoxAddress As BoundField
'TextBoxAddress.DataField = grdHours.Rows(rowcount).Cells(3).FindControl("Date").ToString
Dim Hours As TextBox = DirectCast(grdHours.Rows(rowIndex).Cells(4).FindControl("TextBox1"), TextBox)
Dim Minutes As DropDownList = DirectCast(grdHours.Rows(rowIndex).Cells(5).FindControl("DropDownList2"), DropDownList)
Dim Activity As DropDownList = DirectCast(grdHours.Rows(rowIndex).Cells(6).FindControl("DropDownList1"), DropDownList)
Dim AddEntries As Button = DirectCast(grdHours.Rows(rowIndex).Cells(7).FindControl("btnHoursRow"), Button)
drCurrentRow = dtCurrentTable.NewRow()
' drCurrentRow("RowNumber") = i + 1
dtCurrentTable.Rows(i - 1)("Task") = TaskList.Text.ToString
dtCurrentTable.Rows(i - 1)("Weekday") = grdHours.Rows(rowIndex).Cells(2).Text.ToString
dtCurrentTable.Rows(i - 1)("Date") = grdHours.Rows(rowIndex).Cells(3).Text.ToString
dtCurrentTable.Rows(i - 1)("Hours") = Hours.Text.ToString
dtCurrentTable.Rows(i - 1)("Minutes") = Minutes.SelectedValue.ToString
dtCurrentTable.Rows(i - 1)("Activity") = Activity.SelectedValue.ToString
dtCurrentTable.Rows(i - 1)("AddEntries") = AddEntries.Text.ToString
dtCurrentTable.Rows(i - 1)("AddNotes") = String.Empty
dtCurrentTable.Rows(i - 1)("Validator") = String.Empty
dtCurrentTable.Rows(i - 1)("ErrMessage") = String.Empty
dtCurrentTable.Rows(i - 1)("TimeNotes") = String.Empty
rowIndex += 1
Next
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentGridTable") = dtCurrentTable
grdHours.DataSource = dtCurrentTable
grdHours.DataBind()
Dim txn As TextBox = DirectCast(grdHours.Rows(rowIndex).Cells(1).FindControl("TextBox1"), TextBox)
' txn.Focus;
txn.Focus()
End If
End If
enterHours(True)
End Sub
When I click the button, the btnNewEntryRow_Click function is being called, which in turns calls BindGrdHours() successfully but after completion, nothing is happening. The screen still contains the original gridview.
Any ideas on where I'm going wrong? Thanks for looking.

Related

Error when passing values from dataview to datatable rowcell

I have a page with two gridview; one is inside the modal pop up (grdEmpName) and the other is in the page (gvUser). Now, when the user select search button the modal pop up extender will pop up and then search employees. When the user select someone, the selected data should display to the textbox in the another gridview (gvUser).
Everything is working up to selection in modal pop up extender gridview (grdEmpName)but when I select no data pass to the other gridview. (gvUser). I need to display the selected data in the textbox.
ASPX:
<asp:GridView ID="gvUser" runat="server" Width="1024px"
PageSize="10"
AutoGenerateColumns = "False"
AllowSorting="true"
AllowPaging="true"
ShowFooter="True"
OnSorting="gvUser_Sorting"
OnPageIndexChanging="gvUser_PageIndexChanging"
OnRowCreated="gvUser_RowCreated"
CssClass="Grid"
AlternatingRowStyle-CssClass="alt"
PagerStyle-CssClass="pgr"
FooterStyle-HorizontalAlign="center" >
<PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageImageUrl="~/images/gridview/first_16x16.png" LastPageImageUrl="~/images/gridview/last_16x16.png" />
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/gridview/file_edit_24x24.png" title = "Edit" _CommandArgument='<%# Eval("USR_ID") & "|Edit|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/gridview/file_save_24x24.png" visible="false" title="Update" _CommandArgument='<%# Eval("USR_ID") & "|Update|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/gridview/file_cancel_24x24.png" Visible="false" title="Cancel" _CommandArgument='<%# Eval("USR_ID")& "|Cancel|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/gridview/file_delete_24x24.png" title="Delete" _CommandArgument='<%# Eval("USR_ID")& "|Delete|" & CType(Container,GridViewRow).RowIndex & "|" & Eval("USR_IDNTY") %>' />
<asp:ImageButton ID="btnSearchUser" runat="server" ImageUrl="~/images/gridview/file_search_24x24.png" title="Search" _CommandArgument= "btnSearchUser" OnClick = "btnSearchUser_Click" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="ibtnAdd" runat="server" ImageUrl="~/images/gridview/file_add_24x24.png" title="Add" _CommandArgument='<%# Eval("USR_ID") & "|Add|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User Identity" visible ="false" >
<ItemTemplate>
<asp:Label ID="lblUSR_IDNTY" runat="server" Text='<%# Bind("USR_IDNTY")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblUSR_IDNTY" runat="server" Width="95%" Text='<%# Bind("USR_IDNTY")%>' />
</EditItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User ID" SortExpression="USR_ID">
<ItemTemplate>
<asp:Label ID="lblUSR_ID" runat="server" Text='<%# Bind("USR_ID")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtUSR_ID" runat="server" Width="95%" Text='<%# Bind("USR_ID")%>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtUSR_ID" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="NAME">
<ItemTemplate>
<asp:Label ID="lblNAME" runat="server" Text='<%# Bind("NAME")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNAME" runat="server" Width="95%" Text='<%# Bind("NAME")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNAME" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="EMAIL">
<ItemTemplate>
<asp:Label ID="lblEMAIL" runat="server" Text='<%# Bind("EMAIL")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEMAIL" runat="server" Width="95%" Text='<%# Bind("EMAIL")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEMAIL" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Active" SortExpression="ACTIVE">
<ItemTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Checked ='<%# Bind("ACTIVE")%>' Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Width="95%" Checked ='<%# Bind("ACTIVE")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False"/>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div style="height:315px;border-style:hidden;" >
<table class="table" >
<tr>
<td style="padding:5px;" >
<img id="Img1" style="height:24px;width:24px; " runat="server" src="images/no_data_64x64.png" />
</td>
<td style="padding:5px;font-size:large;">
<b>No data found.</b>
</td>
</tr>
</table>
</div>
</EmptyDataTemplate>
<PagerStyle CssClass="pgr" />
<AlternatingRowStyle CssClass="alt" />
<FooterStyle HorizontalAlign="Center" />
<EmptyDataRowStyle CssClass="EmptyData" />
</asp:GridView>
</div>
<%-- Picker--%>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<cc1:ModalPopupExtender runat="server" TargetControlID="hdControl" ID="mdlEmpListProcess" PopupControlID="empPanel" CancelControlID="btnClose" OnCancelScript="getFlickerSolved();" >
</cc1:ModalPopupExtender>
<asp:HiddenField runat="server" ID="hdControl" />
<asp:Panel runat="server" ID="empPanel" Width="909px" Height="300" BorderStyle="Ridge" BorderColor="Gray" Style="display:none;" BackColor="Control">
<table>
<tr>
<td>
<asp:TextBox runat="server" id="txtEmpName" CssClass="textbox" Width="240"></asp:TextBox>
<asp:Button runat="server" ID="btnSearchEmp" CssClass="button" Text="SEARCH" OnClientClick="getFlickerSolved();"/>
<asp:Button runat="server" ID="btnClose" CssClass="button" Text="CLOSE" />
</td>
</tr>
</table> <br />
<div style="overflow:scroll; width:882px; height:233px;">
<asp:GridView runat="server" ID="grdEmpName" Width="863px" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" Height="35px" GridLines="Vertical">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton runat="server" ID="optEmp" AutoPostBack="true" _CommandArgument= "Select" OnCheckedChanged="selectedEmployee" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="USR_ID" HeaderText="USER ID"/>
<asp:BoundField DataField="NAME" HeaderText="NAME"/>
<asp:BoundField DataField="EMAIL" HeaderText="EMAIL ADDRESS">
<HeaderStyle Width="300px" />
</asp:BoundField>
</Columns>
<RowStyle CssClass="rowStyle" />
<HeaderStyle CssClass="headerStyle" BackColor="Black" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</div>
</asp:Panel>
<%-- End Picker--%>
Now, here's may code for selectedEmployee (Radio Button)
Protected Sub selectedEmployee(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lnBTNDone As RadioButton = DirectCast(sender, RadioButton)
Dim row As GridViewRow = DirectCast(lnBTNDone.NamingContainer, GridViewRow)
Dim RowIndex As Integer = gvUser.EditIndex
Dim dt As New DataTable
dt = TryCast(Session("dbCache_User"), DataView).Table.Copy
Dim dv As New DataView(dt, "", "USR_IDNTY", DataViewRowState.OriginalRows)
Dim s As String = TryCast(gvUser.Rows(RowIndex).FindControl("lblUSR_IDNTY"), Label).Text
Dim idx As Integer = dv.Find(s)
dv(idx)("USR_ID") = row.Cells(1).Text
dv(idx)("NAME") = row.Cells(2).Text
dv(idx)("EMAIL") = row.Cells(3).Text
gvUser.DataSource = dv
gvUser.DataBind()
Me.mdlEmpListProcess.Hide()
End Sub
But I've encounter this error when debugging system.data.dataexception was unhandled by user code. Cannot set Name when passing the value.
dv(idx)("NAME") = row.Cells(2).Text
as per you comment NAME is readonly field error given at runtime than you make is readonly false as below , add same line in your code after reading value
dt.Columns["NAME"].ReadOnly = false
Working example
DataTable dataTable = new DataTable("dataTable");
DataColumn dataColumn = new DataColumn("dataColumn");
dataTable.Columns.Add(dataColumn);
// Add ten rows.
DataRow dataRow;
for (int i = 0; i < 10; i++)
{
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "item " + i;
dataTable.Rows.Add(dataRow);
}
dataTable.AcceptChanges();///add this line in your code
var dv = new DataView(dataTable, "", "dataColumn", DataViewRowState.OriginalRows);
int index = dv.Find("item 1");
if (index == -1)
Console.WriteLine("Product not found");
else
dv[index]["dataColumn"] = "item 1000";
I suggest first check you are able to find row or not
int idx = dv.Find(s);
if (index == -1)
{
Console.WriteLine ("Product not found");
}
else
{
dv[index]["columnname"] = "value";
}

How to Enable a DropDownList in Gridview When Selected the First DropDownList

I'm working with my GridView table in ASP.Net.
First when I click the Edit link button in my GridView I will be able to edit it with Textbox and dropdownlist.
Here's the image of my GridView.
So the Business Unit was enabled but the Division, Sub-Division, Classification and Sub-Classification are not.
My problem is that when I select the Business Unit, the dropdownlist Division should be enabled and be able to select the value according to the Business Unit that I selected and so on. So the next dropdownlist will be dependent on the first dropdownlist that you selected. I searched about this on the internet already and tried different solutions but I'm still confused and don't know how it will work.
Here's my code in ASP.Net
<asp:GridView ID="gvGroup" runat="server" AllowPaging="false"
AllowSorting="true" AutoGenerateColumns="False" BorderColor="Silver"
BorderWidth="1px" Height="104px" ShowFooter="true" ShowHeader="true"
style="margin-right: 0px" Width="77%">
<RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderStyle-BackColor="#666666"
HeaderStyle-ForeColor="White" HeaderStyle-Width="10px" HeaderText=""
ItemStyle-Width="10px">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle BackColor="#666666" ForeColor="White" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblMasterID" runat="server" Text='<%#Bind("MASTERID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblSEQID" runat="server" Text='<%#Bind("SEQID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Job Title" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblJobTitle" runat="server" Text='<%#Bind("JOBTITLE") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJT" runat="server" CssClass="ehr_textbox"
Text='<%#BIND("JOBTITLE") %>' Width="140px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtJobTitleAdd" runat="server" Width="110px">
</asp:TextBox>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Business Unit"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblBusinessUnit" runat="server"
Text='<%#Bind("BUSINESS_UNIT") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBusinessUnitAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Division" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblDivision" runat="server" Text='<%#Bind("DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Division"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubDivision" runat="server" Text='<%#Bind("SUB_DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblClassification" runat="server"
Text='<%#Bind("CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubClassification" runat="server"
Text='<%#Bind("SUB_CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" ItemStyle-Font-Size="9pt">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandName="Delete"
OnClientClick="return confirm('Do you want to delete?')" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnAdd" runat="server" CommandName="Add"
OnClick="AddgvGroup" Text="Add"></asp:LinkButton>
</FooterTemplate>
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:TemplateField>
<asp:CommandField HeaderStyle-BackColor="#666666" HeaderStyle-Width="10px"
ItemStyle-Font-Size="9pt" ItemStyle-Width="10px" ShowEditButton="True">
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:CommandField>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
<EmptyDataTemplate>
<div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
No record found.
</div>
</EmptyDataTemplate>
<HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial"
Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
And here's my code in VB.Net
Protected Sub gvGroup_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGroup.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlBusinessUnit As DropDownList = DirectCast(e.Row.FindControl("ddl_gvBusinessUnit"), DropDownList)
'bind dropdown-list
Dim sqlstr As String
Dim dt As DataTable = New DataTable()
sqlstr = "Select BUSINESS_UNIT from BUSINESS_UNIT_TBL"
dt = ehr_utils.DataTable(sqlstr)
ddlBusinessUnit.DataSource = dt
ddlBusinessUnit.DataTextField = "BUSINESS_UNIT"
ddlBusinessUnit.DataValueField = "BUSINESS_UNIT"
ddlBusinessUnit.DataBind()
Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
ddlBusinessUnit.SelectedValue = dr("BUSINESS_UNIT").ToString()
End If
End If
End Sub
Any idea of how can I possibly do that? And I tried also the OnSelectedIndexChanged
First you need to add an OnSelectedIndexChanged to ddl_gvBusinessUnit and set AutoPostBack to true
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_gvBusinessUnit_SelectedIndexChanged">
And then in code behind
protected void ddl_gvBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
//cast the sender back to a dropdownlist
DropDownList dropDownList1 = sender as DropDownList;
//get the selectedvalue
string value = dropDownList1.SelectedValue;
//find the other dropdownlist in the correct row by using the editindex
DropDownList dropDownList2 = gvBusinessUnit.Rows[gvBusinessUnit.EditIndex].FindControl("DropDownList2") as DropDownList;
//do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red;
}
VB
Protected Sub ddl_gvBusinessUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
'cast the sender back to a dropdownlist
Dim dropDownList1 As DropDownList = CType(sender,DropDownList)
'get the selectedvalue
Dim value As String = dropDownList1.SelectedValue
'find the other dropdownlist in the correct row by using the editindex
Dim dropDownList2 As DropDownList = CType(gvBusinessUnit.Rows(gvBusinessUnit.EditIndex).FindControl("DropDownList2"),DropDownList)
'do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red
End Sub

export gridview to excel is not working with Ajax and Update panel

I have a grid-view in Update panel. there is a link button inside grid-view which fire export command in row command event. There is an another grid-view which get exported on this command code. I debug, the pointers goes in but the export not happening. I know i am missing something here, but not able to get it after spending lot of hours. The Script manager is on master page.
Can any one help please. Below is the code.
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:GridView ID="grd_Report" runat="server" ShowFooter="true" CssClass="ui-jqgrid-btable table-hover" AutoGenerateColumns="false" AllowPaging ="true" PageSize ="12" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_CampaignName" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="CreateDate">
<ItemTemplate>
<asp:Label ID="lbl_CreateDate" runat="server" Text='<%#Eval("CreateDate") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="130px" />
<HeaderStyle Width="130px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="sender">
<ItemTemplate>
<asp:Label ID="lbl_senderID" runat="server" Text='<%#Eval("sender") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="85px" />
<HeaderStyle Width="85px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList class="ui-pg-selbox" ID="drp_itemCount" runat="server" AutoPostBack="true"
onselectedindexchanged="drp_itemCount_SelectedIndexChanged" >
<asp:listItem>10</asp:listItem>
<asp:listItem>15</asp:listItem>
<asp:listItem>50</asp:listItem>
<asp:listItem>100</asp:listItem>
<asp:listItem>200</asp:listItem>
</asp:DropDownList>
</FooterTemplate>
<ItemStyle Width="375px" />
<HeaderStyle Width="375px" />
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<asp:Label ID="lbl_SMSCount" runat="server" Text='<%#Eval("Count") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="80px" />
<HeaderStyle Width="80px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Delivery Count">
<ItemTemplate>
<table class="deliveryCount">
<tr>
<td style="padding:5px;width:180px" colspan="2">Total: <%#Eval("Total")%></td>
</tr>
<table style="font-size:11px;width:180px;">
<tr>
<td style="padding:5px;font-size:11px;width:80px">A: <%#Eval("A")%></td>
<td style="padding:5px;font-size:11px;width:80px">B: <%#Eval("B")%></td>
</tr
<tr>
<td style="padding:5px;font-size:11px;width:80px">2: <%#Eval("2")%></td>
<td style="padding:5px;font-size:11px;width:80px">1: <%#Eval("1")%></td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnk_download" style="font-size:11px;" CommandName="download" CommandArgument='<%#Eval("CampaignId") %>' text="Download" class="ui-icon-document"></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" width="100px" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" Font-Size="13" Font-Bold="true" />
<RowStyle Font-Names="Helvetica" Font-Size="10" />
<PagerStyle CssClass ="gridviewPager" Height="30px" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="grd_export" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel6" runat="Server">
<ContentTemplate>
<asp:GridView ID="grd_export" runat="server" Visible="false" AutoGenerateColumns ="false" >
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Number">
<ItemTemplate>
<asp:Label ID="lbl_Number" runat="server" Text='<%#Eval("Number") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Id">
<ItemTemplate>
<asp:Label ID="lbl_Id" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:Label ID="lbl_text" runat="server" Text='<%#Eval("Text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="text Id">
<ItemTemplate>
<asp:Label ID="lbl_MessageId" runat="server" Text='<%#Eval("textId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lbl_Date" runat="server" Text='<%#Eval("Date") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="lbl_Status" runat="server" Text='<%#Eval("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credits">
<ItemTemplate>
<asp:Label ID="lbl_Credits" runat="server" Text='<%#Eval("Credits") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Below is the code on Row Command event
If e.CommandName = "download" Then
Dim str As String
str = " select * FROM [Box] where MsgId='" + e.CommandArgument + "'"
Dim com As New SqlCommand(str, con)
Dim da As New SqlDataAdapter(com)
Dim dt As New DataTable
da.Fill(dt)
If Not dt Is Nothing Then
If dt.Rows.Count > 0 Then
grd_export.DataSource = dt
grd_export.DataBind()
grd_export.Visible = True
Response.Clear()
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.AddHeader("content-disposition", "attachment;filename=Message.xls")
Response.Charset = ""
Response.ContentType = "application/vnd.xls"
Me.EnableViewState = False
Dim stringwrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlwrite As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringwrite)
grd_export.RenderControl(htmlwrite)
Response.Write(stringwrite.ToString())
grd_export.Visible = False
Response.End()
End If
End If
End If
You need to add a postback trigger to your update panel for respose.write to work.

Input Value to Item template with diffrent query and get parameter value from the gridview

i Want to add on column on aspx page, and the value is comes from diffrent query,
here's my datagridview on aspx :
<asp:Panel ID="pnlGrid" runat="server" align="center">
<asp:Label ID="lbldgTime" runat="server" Text=" "></asp:Label><br />
<asp:GridView ID="gvData" runat="server" AllowPaging="True" AutoGenerateColumns="False"
EmptyDataText="No data available." CellPadding="4" DataKeyNames="PayId" Font-Size="13px"
ForeColor="Black" PageSize="20" Width="100%" Style="text-align: center">
<Columns>
<asp:TemplateField>
<ItemStyle HorizontalAlign="Center" Width="20px" />
<FooterStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:ImageButton ID="imgbtn" ImageUrl="~/Img/view.png" runat="server" Width="25"
Height="25" OnClick="imgbtn_Click" />
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="No Account">
<ItemStyle HorizontalAlign="Center" Width="100px" />
<FooterStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblNoaccount" runat="server" Text='<%# Bind("PayAccount") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<ItemStyle HorizontalAlign="Center" Width="150px" />
<FooterStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblCustName" runat="server" Text='<%# Bind("PayCustName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemStyle HorizontalAlign="Center" Width="20px" />
<FooterStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text='<%# Bind("PayAmount") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount Paid">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<FooterStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblRefNo" runat="server" Text='<%# Bind("PayRefNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Response">
<ItemStyle HorizontalAlign="Center" Width="70px" />
<FooterStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblResponse" runat="server" Text='<%# Bind("PayResponse") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
the field :
<asp:TemplateField HeaderText="Response">
<ItemStyle HorizontalAlign="Center" Width="70px" />
<FooterStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:Label ID="lblResponse" runat="server" Text='<%# Bind("PayResponse") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
the value is comes from diffrent query when fill the datagrid,
and here's the code behind of my datagrid :
Sub FillData()
Try
Dim dt As New DataTable
Me.lbleMessage.Text = ""
If txtDate.Text = "__/__/____" Then
txtDate.Text = ""
End If
If CPayment.SearchPayment(txtAccount.Text.Trim, txtCustName.Text.Trim, txtAmount.Text, txtAmountPaid.Text, dropResponse.SelectedValue.ToString, txtRefNo.Text.Trim, txtDate.Text) Then
dt = CPayment.DT
Else
eMessage("System failure: ", CPayment.eMsg)
End If
Dim RC As Integer = dt.Rows.Count
totalrows = RC
If RC = 0 Then
'dt.Rows.Add()
dt.Clear()
gvData.DataSource = dt.DefaultView
gvData.DataBind()
eMessage("", "There are no row to display.")
Else
gvData.DataSource = dt.DefaultView
gvData.DataBind()
gvData.BottomPagerRow.Visible = True
End If
Catch ex As Exception
eMessage("Load Data failure: ", ex.ToString())
End Try
End Sub
and here's the query for the response field :
Public Function Revstatus(ByVal PayRefNo As String) As Boolean
Dim strsql As String = " Select COUNT(*) from payment(Pay) inner(Join) Reversal(Rev) on Pay.PayRefNo = Rev.PayRefNo where Pay.PayRefNo ='%" & PayResponse & "%'"
Return runQuery(strsql)
End Function
the ByVal PayRefNo As String parameter comes from
<asp:TemplateField HeaderText="Amount Paid">
<ItemStyle HorizontalAlign="Center" Width="40px" />
<FooterStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:Label ID="lblRefNo" runat="server" Text='<%# Bind("PayRefNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
is it possible to input differnce query on datagrid while fill it ?
ahhhhh... finnaly i found the way.
on datagridview row bound i did this :
If e.Row.RowType = DataControlRowType.DataRow Then
Dim RefNo As Label = CType(e.Row.FindControl("lblRefNo"), Label)
If CPayment.Revstatus(RefNo.Text) Then
CType(e.Row.FindControl("lblRevResp"), Label).Text = "Reversed"
Else
CType(e.Row.FindControl("lblRevResp"), Label).Text = "Paid"
End If
End If

Search gridview ItemTemplate textbox value on buttonclick

My Scenario is that i had a gridview,i had taken itemtemplate textboxes and a button,when the gridview is loaded the user enters a ServiceCode based on the ServiceCode and on button click,data should be displayed into the gridview,when the data is displaye in the gridview the user enters the quantity and the discount then the result should be displayed in the netamount,i had written the code but it is not working i would appreciate if someone share thier knowledge with me.
My Axpx Code is:
<asp:GridView ID="GridView1" runat="server" Height="157px" Width="639px"
AutoGenerateColumns="False" BorderWidth="1px"
HorizontalAlign="Justify" onrowdatabound="GridView1_RowDataBound"
BackColor="LightGoldenrodYellow" BorderColor="Tan" CellPadding="2"
ForeColor="Black" GridLines="None" onrowcommand="GridView1_RowCommand">
<FooterStyle BackColor="Tan" />
<PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
<HeaderStyle BackColor="#FFFFC4" Font-Bold="True" ForeColor="#BF6000"/>
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<asp:Label ID="label10" runat="server" Font-Bold="true" ForeColor="#BF6000" Text="<%# Container.DataItemIndex + 1 %>">
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serv Code" >
<ItemTemplate>
<asp:TextBox ID="TxtServiceCode" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.ServiceCode") %>'
runat="server" Width="55px">
</asp:TextBox>
<asp:Button ID="Insert" runat="server" BackColor="Ivory" CommandName="InsertRecord"
Font-Bold="True" ForeColor="#BF6000" Text="Insert" onclick="Insert_Click"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:TextBox ID="TxtName" runat="server" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" Width="150px" Text='<%# DataBinder.Eval(Container, "DataItem.ServiceName") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Serv Amt">
<ItemTemplate>
<asp:TextBox ID="TxtServiceAmount" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.ServiceAmount") %>'
runat="server" Width="55px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Qty">
<ItemTemplate>
<asp:TextBox ID="TxtQuantity" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.Quantity") %>'
runat="server" Width="55px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Disc Amt">
<ItemTemplate>
<asp:TextBox ID="TxtdiscAmt" Font-Bold="true" ForeColor="#BF6000" Text ='<%# DataBinder.Eval(Container,"DataItem.Discount") %>'
AutoPostBack="true" OnTextChanged="TxtdiscAmt_TextChanged" runat="server" Width="55px"> </asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Net Amt">
<ItemTemplate>
<asp:TextBox ID="lblNet" runat="Server" Font-Bold="true" ForeColor="#BF6000" Width="55" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="PaleGoldenrod" />
</asp:GridView>
MY CS Code is:
DataSet dss = new DataSet();
SqlConnection MyConnection = new SqlConnection("server=prog;database=mydatabase; UID=sa;PWD=nato123;");
SqlCommand sqlcmd = new SqlCommand("select * from [Bill]", MyConnection);
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
Instead of using the button OnClick event, I would use OnCommand and pass the item index in the CommandArgument:
<asp:Button ID="Button1" runat="server" OnCommand="Button1_Command" CommandArgument='<%#Container.ItemIndex%>' />
Code-behind:
protected void Button1_Command(object sender, CommandEventArgs e)
{
GridViewRow row = GridView1.Rows[(int)e.CommandArgument];
if (row != null)
{
TextBox txt = row.FindControl("TextBox1") as TextBox;
if (txt != null)
{
string value = txt.Text;
}
}
}

Resources