2017-04-26 94 views
0

我很難搞清楚如何從靜態ID獲取值。當我點擊它時,我有一些將標籤轉換爲文本框的jquery。它使用標籤的(靜態)ID並將其轉換爲TextBox。 所以爲前:卷標=> TextBox1的如何從gridview上的文本框中獲取靜態ID的特定值C#

我想知道我怎麼只能從特定的細胞得到TextBox1的價值,因爲當我嘗試更新它所有的文本框從列的值(與id:Label1(afspraken))比在TextBox1。所以當我更新時,整列的所有值都在單元格中(如果不清楚,請看代碼下方的圖像)

任何幫助都是值得歡迎的,因爲我需要明天交出這個項目,非常高興得到這個工作!

代碼:

C#

 protected void Button3_Click(object sender, EventArgs e) 
    { 
     _controller = new Controller(); 

     //Variablen 
     string afspraak =""; 
     string uitleg =""; 
     string id=""; 
     string id1 = ""; 

     //Values uit uit gridview halen halen 
     foreach (GridViewRow row in GridView1.Rows) 
     { 
      string tekst = Request.Form["TextBox1"]; 
      /afspraak = ((TextBox)row.FindControl("txtEditTabel")).Text; 

      //Don't look at this 
      uitleg = ((Label)row.FindControl("Label2")).Text; 
      id = ((Label)row.FindControl("Label3")).Text; 
      id1 = ((Label)row.FindControl("Label4")).Text; 
     } 

     //Methode om record te bewerken 
     _controller.RecordUpdatenTblAfspraken(afspraak, uitleg, Convert.ToInt32(id), Convert.ToInt32(id1)); 

     //Pagina refreshen 
     Response.Redirect(Request.RawUrl); 

    } 

ASP.NET GRIDVIEW

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1000px" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDeleting="GridView1_RowDeleting" DataKeyNames="IDAfspraken" ClientIDMode="Static"> 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
      <asp:TemplateField HeaderText="IDAfspraken"> 
       <ItemTemplate> 
        <asp:Label ID="Label4" runat="server" Text='<%# Eval("IDAfspraken") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Afspraak"> 
       <ItemTemplate> 
        <asp:Label ID="Label1" CssClass="editable" runat="server" Text='<%# Eval("Afspraak") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Uitleg"> 
       <ItemTemplate> 
        <asp:Label ID="Label2" CssClass="editable" runat="server" Text='<%# Eval("Uitleg") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Categorie"> 
       <ItemTemplate> 
        <asp:Label ID="Label3" CssClass="editable" runat="server" Text='<%# Eval("IDCategorieën") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="Bewerken"> 
       <ItemTemplate> 
        <asp:Button ID="Button3" runat="server" Text="Bewerken" ForeColor="DeepSkyBlue" Font-Bold="true" OnClick="Button3_Click"/> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:CommandField ButtonType="Button" ShowDeleteButton="True" /> 
     </Columns> 
     <EditRowStyle BackColor="#999999" /> 
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
     <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
     <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
    </asp:GridView> 

JAVASCRIPT

$(function() { 
//Loop through all Labels with class 'editable'. 
$(".editable").each(function() { 
    //Reference the Label. 
    var label = $(this); 

    //Add a TextBox next to the Label. 
    label.after("<input type = 'text' style = 'display:none' />"); 

    //Reference the TextBox. 
    var textbox = $(this).next(); 

    //Set the name attribute of the TextBox. 
    var id = this.id.split('_')[this.id.split('_').length - 1]; 
    textbox[0].name = id.replace("Label","Textbox"); 

    //Assign the value of Label to TextBox. 
    textbox.val(label.html()); 

    //When Label is clicked, hide Label and show TextBox. 
    label.click(function() { 
     $(this).hide(); 
     $(this).next().show(); 
    }); 

    //When focus is lost from TextBox, hide TextBox and show Label. 
    textbox.focusout(function() { 
     $(this).hide(); 
     $(this).prev().html($(this).val()); 
     $(this).prev().show(); 
    }); 
}); 

enter image description here

+0

看看[本教程](http://www.ezzylearning.com/tutorial/editing-data-using-asp-net-gridview-control)。它涵蓋了GridView編輯和更新的所有基礎知識。 – VDWWD

回答

1

而不是使用jquery你應該使用gridview的內置編輯屬性。 下面是一個示例代碼 ASPX頁面

<asp:GridView ID="gridmeta" runat="server" AutoGenerateColumns="False" OnRowEditing="gridmeta_RowEditing" OnRowCancelingEdit="gridmeta_RowCancelingEdit" OnRowUpdating="gridmeta_RowUpdating" DataKeyNames="metaid"> 
     <asp:TemplateField HeaderText="Content"> 
      <ItemTemplate> 
       <label><%#Eval("metacontent") %></label> 
       </ItemTemplate> 
       <EditItemTemplate> 
        <asp:TextBox TextMode="MultiLine" Rows="3" Columns="60" runat="server" ID="txtcontent" Text='<%#Eval("metacontent") %>'></asp:TextBox> 
       </EditItemTemplate> 
      </asp:TemplateField> 
      <asp:CommandField ShowEditButton="true" /> 
     </Columns> 
    </asp:GridView> 

CS文件

protected void gridmeta_RowEditing(object sender, GridViewEditEventArgs e) 
{ 
    gridmeta.EditIndex = e.NewEditIndex; 
    fillGrid(); 
} 


protected void gridmeta_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) 
{ 
    gridmeta.EditIndex = -1; 
    fillGrid(); 
} 


protected void gridmeta_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    int index; 
    index = e.RowIndex; 
    int metaid = Convert.ToInt32(gridmeta.DataKeys[index].Value.ToString()); 
    string content = ((TextBox)gridmeta.Rows[index].FindControl("txtcontent")).Text; 
    //Your Code to update an entry based on id in this case metaid 
} 

此外,如果有你想改變一個以上的列值只是把edititemtemplate在他們和gridmeta_RowUpdating功能訪問它們。

相關問題