我有一个名为Run Time
的列的GridView,它使用TextBoxes rTime
。我将在andriod操作系统上使用这个GridView,如果有人点击文本框,则希望它拉动键盘。这在没有GridView的情况下已经有效,但我一直遇到The name 'rTime' does not exist in the current context
错误。这是我的aspx代码:`
<asp:TemplateField HeaderText="labelID" Visible="false">
<ItemTemplate>
<asp:Label ID="ID" runat="server" Text='<%# Eval("Id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="labelfirstname" Visible="true" runat="server" Text='<%# Eval("fName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="labellastname" Visible="true" runat="server" Text='<%# Eval("lName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Time Needed">
<ItemTemplate>
<asp:Label ID="labelrTimeN" Visible="true" runat="server" Text='<%# Eval("rTimeN") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run Time">
<ItemTemplate>
<div style="display:none"><asp:TextBox ID="rTime" runat="server" Height="16px"
ontextchanged="TextBox1_TextChanged" Width="108px"></asp:TextBox></div>
<span class="style4"><strong>Meter Run:</strong></span><span class="style5"> </span><input onblur="document.getElementById('<%=rTime.ClientID %>').value = this.value"
type="tel" style="width: 100px; height: 31px;" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>`
这是c#:
private bool isEditMode = false;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
UpdatePanel1.Visible = false;
}
private void BindData()
{
string connectiongString = "Data Source=WSCJTCSQ1;Initial Catalog=TestDB;Persist Security Info=True;User ID=v2soft;Password=passwordv2soft";
SqlConnection myConnection = new SqlConnection(connectiongString);
SqlDataAdapter ad = new SqlDataAdapter("SELECT Id, fName, lName, rTimeN, rTime FROM bleaTest", myConnection);
DataSet ds = new DataSet();
ad.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
isEditMode = true;
UpdatePanel1.Visible = true;
BindData();
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
Update();
}
protected bool IsInEditMode
{
get { return this.isEditMode; }
set { this.isEditMode = value; }
}
private void Update()
{
StringBuilder sb = new StringBuilder();
foreach (GridViewRow row in GridView1.Rows)
{
sb.Append("UPDATE bleaTest SET rTime = '");
sb.Append((row.FindControl("rTime") as TextBox).Text);
sb.Append("'");
sb.Append(" WHERE id = ");
sb.Append(Convert.ToInt32((row.FindControl("ID") as Label).Text));
sb.Append(" ");
}
string connectiongString = "Data Source=WSCJTCSQ1;Initial Catalog=TestDB;Persist Security Info=True;User ID=v2soft;Password=passwordv2soft";
SqlConnection myConnection = new SqlConnection(connectiongString);
SqlCommand myCommand = new SqlCommand(sb.ToString(), myConnection);
myConnection.Open();
myCommand.ExecuteNonQuery();
myConnection.Close();
isEditMode = false;
BindData();
UpdatePanel1.Visible = false;
}
protected void gvUsers_RowDataBound(object sender, GridViewCommandEventArgs e)
{
}
提前谢谢!!
答案 0 :(得分:0)
做了一些研究并重新编写了一些代码。这是更新:
<asp:TemplateField HeaderText="Run Time">
<ItemTemplate>
<div style="display:none"> <asp:TextBox ID="rTime" runat="server" type="number" Text='<%# Eval("rTime") %>' ></asp:TextBox></div>
<input onblur="document.getElementById('<%# ((GridViewRow)Container).FindControl("rTime").ClientID %>').value = this.value"
type="tel" style="width: 100px; height: 31px;" />
</ItemTemplate>