2012-11-07 10 views
0

我有一個名爲Run Time的GridView,它使用TextBoxes rTime。我將在一個andriod操作系統上使用這個GridView,並希望它在有人點擊文本框時拉出一個鍵盤。這之前沒有GridView的工作,但我已經得到一個The name 'rTime' does not exist in the current context錯誤。這裏是我的aspx代碼:` 帶有強制鍵盤的GridView文本框

<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">&nbsp;</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

做了一些更多的研究並重新編寫了一些代碼。這是更新:

<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>