这个确切的代码适用于另一个页面,但不适用于我正在处理的页面。我从我的其他页面复制并粘贴它,但无法弄清楚什么是错误的。希望它很容易。
我有一个gridview,它有一些下拉列表和文本框。当我单击btnApproveSave按钮时,它应该读取gridview中的数据,然后对数据库执行更新。但是,c#不会为gridview的每一行中的下拉列表或文本框读取任何内容。
<asp:Button ID="btnApprove" runat="server" OnClick="btnApprove_Click" Text="Submit Approval" />
<asp:Button ID="btnApproveSave" runat="server" Text="Save Entries" OnClick="btnApproveSave_Click" />
<br />
<asp:Label ID="lblError" runat="server" Text="Please Verify That All Fields Have Been Filled Out" Visible="false" ForeColor="#D71826" />
<br />
<br />
<div class="form-group">
<asp:GridView ID="gvCreditCards" runat="server" Visible="true" ShowHeaderWhenEmpty="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="id" HeaderText="ID" ItemStyle-CssClass="gvCCPadding" />
<asp:BoundField DataField="fin_transaction_date" HeaderText="Transaction Date" ItemStyle-CssClass="gvCCPadding" ItemStyle-Width="50%" DataFormatString="{0:MM/dd/yyyy}" />
<asp:BoundField DataField="acc_account_name" HeaderText="Account Name" ItemStyle-CssClass="gvCCPadding minWidth" />
<asp:BoundField DataField="fin_charge" HeaderText="Charge" ItemStyle-CssClass="gvCCPadding" DataFormatString="{0:C}" ItemStyle-HorizontalAlign="Right" />
<asp:BoundField DataField="fin_transaction_description" HeaderText="Vendor Name" ItemStyle-CssClass="gvCCPadding minWidth" />
<asp:BoundField DataField="mch_state_province" HeaderText="State" ItemStyle-CssClass="gvCCPadding" />
<asp:TemplateField HeaderText="Description" ControlStyle-Width="150px">
<ItemTemplate>
<asp:TextBox ID="txtUserDescription" runat="server" Text='<%# Eval("toa_user_description") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Receipt (Y/N)" ControlStyle-Width="75px">
<ItemTemplate>
<asp:DropDownList ID="ddlReceipt" runat="server">
<asp:ListItem Text="" Selected="True" Value=""></asp:ListItem>
<asp:ListItem Text="Yes" Selected="False" Value="Yes"></asp:ListItem>
<asp:ListItem Text="No" Selected="False" Value="No"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="GL Code" ControlStyle-Width="200px">
<ItemTemplate>
<asp:DropDownList ID="ddlGLCode" runat="server" DataSourceID="ds_AccountCode" DataValueField="toa_account_name" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="" Value=""></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department" ControlStyle-Width="200px">
<ItemTemplate>
<asp:DropDownList ID="ddlDepartment" runat="server" DataSourceID="ds_Department" DataValueField="toa_businessDepartment" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="" Value=""></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" ControlStyle-Width="200px">
<ItemTemplate>
<asp:DropDownList ID="ddlLocation" runat="server" DataSourceID="ds_businessLocation" DataValueField="toa_site" AppendDataBoundItems="true">
<asp:ListItem Selected="True" Text="" Value=""></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="#e9e9e9" />
<HeaderStyle BackColor="#e9e9e9" ForeColor="#D71826" />
<EmptyDataTemplate>
Empty Gridview
</EmptyDataTemplate>
<EmptyDataRowStyle ForeColor="#D71826" />
</asp:GridView>
</div>
</div>
</div>
<asp:SqlDataSource ID="ds_AccountCode" runat="server" ConnectionString="<%$ ConnectionStrings:TOA_LIVEConnectionString %>"
SelectCommand="SELECT [toa_account_name] FROM [toa_AccountCode]"></asp:SqlDataSource>
<asp:SqlDataSource ID="ds_Department" runat="server" ConnectionString="<%$ ConnectionStrings:TOA_LIVEConnectionString %>"
SelectCommand="SELECT [toa_businessDepartment] FROM [toa_Department]"></asp:SqlDataSource>
<asp:SqlDataSource ID="ds_businessLocation" runat="server" ConnectionString="<%$ ConnectionStrings:TOA_LIVEConnectionString %>"
SelectCommand="SELECT [toa_site] FROM [toa_Location]"></asp:SqlDataSource>
</asp:Content>
C#
protected void btnApprove_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gvCreditCards.Rows)
{
string id = row.Cells[0].Text;
TextBox tDESCRIPTION = (TextBox)row.FindControl("txtUserDescription");
string sDESCRIPTION = tDESCRIPTION.Text;
DropDownList ddlRECEIPT = (DropDownList)row.FindControl("ddlReceipt");
string sRECEIPT = ddlRECEIPT.SelectedValue;
DropDownList ddlGLCODE = (DropDownList)row.FindControl("ddlGLCode");
string sDDLCODE = ddlGLCODE.SelectedValue;
DropDownList ddlDEPARTMENT = (DropDownList)row.FindControl("ddlDepartment");
string sDEPARTMENT = ddlDEPARTMENT.SelectedValue;
DropDownList ddlLOCATION = (DropDownList)row.FindControl("ddlLocation");
string sLOCATION = ddlLOCATION.SelectedValue;
foreach (var v in new object[] { sDESCRIPTION, sRECEIPT, sDDLCODE, sDEPARTMENT, sLOCATION })
{
if (v.ToString() == "")
{
btnApproveSave_Click(sender, e);
Response.Redirect(string.Format("http://transovaapps/TransOvaApps/departments/Accounting/CreditCard/CreditCardApprovals.aspx?emptyValue=true"));
}
}
}