无法在GridView中读取动态创建的文本框

伊卢利亚斯

新成员
已加入
2018年4月15日
留言内容
1
编程经验
10+
[字体=&quot]Hi
我正在以编程方式在gridview中创建文本框。但是我无法从文本框中获取值。

[/ FONT]
C#:
[字体=&quot][B]ASPX page:[/B][/FONT]
[FONT=&quot]<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="newplan.aspx.cs" Inherits="newplan" %>
<%@ 寄存器 assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajaxToolkit" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<div class="row">
<div class="col-md-4">
<table>
<tr>
<td style="vertical-align:top;">
<table>
<tr>
<th class="single_th" >Date</th>
</tr>
<tr>
<td>
<asp:UpdatePanel ID="upDate" runat="server">
<ContentTemplate>
<div>
<asp:Calendar ID="calDate" runat="server" BackColor="#FFFFCC" BorderColor="#FFCC66" BorderWidth="1px" DayNameFormat="Shortest" Font-Names="Arial" Font-Size="8pt" ForeColor="#663399" Height="200px" ShowGridLines="True" Width="220px">
<DayHeaderStyle BackColor="#FFCC66" Font-Bold="True" Height="1px" />
<NextPrevStyle Font-Size="9pt" ForeColor="#FFFFCC" />
<OtherMonthDayStyle ForeColor="#CC9966" />
<SelectedDayStyle BackColor="#CCCCFF" Font-Bold="True" />
<SelectorStyle BackColor="#FFCC66" />
<TitleStyle BackColor="#990000" Font-Bold="True" Font-Size="9pt" ForeColor="#FFFFCC" />
<TodayDayStyle BackColor="#FFCC66" ForeColor="White" />
</asp:Calendar>
</div>
<div>
<strong>Insert No of Rows:</strong>  <asp:TextBox ID="txtRows" runat="server" Width="30px" />  
<asp:Button ID="btnGo" runat="server" CssClass="buttoncss" Text="Go" OnClick="btnGo_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
</td>
<td>   </td>
<td>
<asp:UpdatePanel ID="pnlIG" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div>
<asp:GridView ID="gvPlan" runat="server"
ShowFooter="True" AutoGenerateColumns="true"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowDeleting="gvPlan_RowDeleting">
<Columns>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
<div style="float:right;">
<asp:Button ID="btnSave" runat="server" Text="Save" CssClass="buttoncss" OnClick="btnSave_Click" />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnGo" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
</div>
</div>
</asp:Content>[/FONT]
[FONT=&quot][B]Code Page:[/B][/FONT]
[FONT=&quot]protected void btnGo_Click(object sender, EventArgs e)
{
Insert_Grid();
}[/FONT]
[FONT=&quot]private void Insert_Grid()
{
CreateGridView();
int numbers = int.Parse(txtRows.Text.Trim());[/FONT]
[FONT=&quot]int cellCount = gvPlan.Rows[0].Cells.Count;
int rowsCount = gvPlan.Rows.Count;[/FONT]
[FONT=&quot]foreach (GridViewRow row in gvPlan.Rows)
{
// radio button - SHIFT
RadioButtonList rbShift = new RadioButtonList();
rbShift.ID = "rShift" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
rbShift.Items.Add(new ListItem("D", "D"));
rbShift.Items.Add(new ListItem("N", "N"));
rbShift.RepeatDirection = RepeatDirection.Horizontal;
rbShift.Attributes.Add("runat", "server");
rbShift.SelectedValue = "D";
row.Cells[0].Controls.Add(rbShift);[/FONT]
[FONT=&quot]// text box - ID
TextBox txtID = new TextBox();
txtID.ID = "tID" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtID.Attributes.Add("runat", "server");
//txtID.Attributes.Add("EnableViewState", "false");
//txtID.Attributes.Add("AutoPostBack", "true");
//txtID.Attributes.Add("OnTextChanged", "txtID_TextChanged");
row.Cells[1].Controls.Add(txtID);[/FONT]
[FONT=&quot]// text box - NAME
TextBox txtName = new TextBox();
txtName.ID = "tName" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtName.Attributes.Add("runat", "server");
txtName.Attributes.Add("ReadOnly", "true");
row.Cells[2].Controls.Add(txtName);[/FONT]
[FONT=&quot]// text box - DESIGNATION
TextBox txtDesig = new TextBox();
txtDesig.ID = "tDesig" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtDesig.Attributes.Add("runat", "server");
txtDesig.Attributes.Add("ReadOnly", "true");
row.Cells[3].Controls.Add(txtDesig);[/FONT]
[FONT=&quot]// text box - EXTN
TextBox txtExtn = new TextBox();
txtExtn.ID = "tExtn" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtExtn.Attributes.Add("runat", "server");
row.Cells[4].Controls.Add(txtExtn);[/FONT]
[FONT=&quot]// text box - MOB
TextBox txtMob = new TextBox();
txtMob.ID = "tMob" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtMob.Attributes.Add("runat", "server");
row.Cells[5].Controls.Add(txtMob);[/FONT]
[FONT=&quot]// text box - REMARKS
TextBox txtRemrks = new TextBox();
txtRemrks.ID = "tRemrks" + (Convert.ToInt32(row.RowIndex + 1)).ToString();
txtRemrks.Attributes.Add("runat", "server");
row.Cells[6].Controls.Add(txtRemrks);[/FONT]
[FONT=&quot]}[/FONT]
[FONT=&quot]
TextBox txn = (TextBox)gvPlan.Rows[0].Cells[1].FindControl("tID1");
txn.Focus();
}[/FONT]
[FONT=&quot]private void CreateGridView()
{[/FONT]
[FONT=&quot]int numbers = int.Parse(this.txtRows.Text.Trim());
DataTable dt = new DataTable();
//you can add as many columns you want
dt.Columns.Add("Shift", typeof(string));
dt.Columns.Add("ID", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Desig", typeof(string));
dt.Columns.Add("Extn", typeof(string));
dt.Columns.Add("Mob", typeof(string));
dt.Columns.Add("Remrks", typeof(string));
for (int i = 0; i < numbers; i++)
{
//dont forget to add null values in each column
dt.Rows.Add("", "", "", "", "", "", "");
}
ViewState["CurrentTable"] = dt;
gvPlan.DataSource = dt;
gvPlan.DataBind();[/FONT]
[FONT=&quot]}[/FONT]
[FONT=&quot]protected void btnSave_Click(object sender, EventArgs e)
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//extract the TextBox values
TextBox gtxtID = (TextBox)gvPlan.Rows[rowIndex].Cells[1].FindControl("tID" + (rowIndex+1));
TextBox gtxtName = (TextBox)gvPlan.Rows[rowIndex].Cells[2].FindControl("tName" + (rowIndex + 1));
gtxtName.Text = gtxtID.Text;
rowIndex++;
}
}
}[/FONT]
[字体=&quot]
"btnSave_Click"程序是我有这个问题的地方。
我要去哪里错了。非常感谢您的帮助。
问候,伊利亚斯[/ FONT]
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,501
地点
悉尼,澳大利亚
编程经验
10+
我不确定是否可以帮上忙,因为我不使用Web窗体,但是通过发布难以阅读的代码,您已经很难提供帮助。确保直接复制和粘贴以保持缩进,这对可读性至关重要。另外,分别发布ASPX和C#代码。将代码仅放在代码格式标记内,然后发布C#代码,如下所示:

[xcode = c#]您的代码在这里[/ xcode]
 
最佳 底部