简体   繁体   English

如何在Button的同一行的数据列表中引用TextBox?

[英]How to refer to a TextBox in a datalist of the same row of the Button?

I want button in the datalist OnClick gets text from both textboxes on the same row of the button.. how can i refer to that using C# keeping in mind that I want to use my own stored procedures and functions "OnClicking" buttons without using SqlDataSource Control 我希望数据列表中的按钮OnClick从按钮的同一行上的两个文本框中获取文本。.我如何使用C#进行引用,同时谨记我想使用自己的存储过程和函数“ OnClicking”按钮而不使用SqlDataSource控制

<asp:DataList ID="DataList1" runat="server">
  <ItemTemplate>
    <table class="auto-style2">
      <tr>
        <td>
          <asp:Label ID="Label1" runat="server" Text='<%# Eval("UDI") %>'></asp:Label>
        </td>
        <td>
          <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
        </td>
        <td>
          <asp:TextBox ID="TextBox2" runat="server" Text='<%# Eval("describtion") %>'></asp:TextBox>
        </td>
        <td>
          <asp:Button ID="Button2" runat="server" Text="Modify" CommandArgument='<%# Eval("UDI") %>' OnCommand="Button2_Command" />
        </td>
      </tr>
    </table>
  </ItemTemplate>
</asp:DataList>

and here is the code behind 这是背后的代码

AdminControlEntities db = new AdminControlEntities();
var x=db.sp_GetAllProducts(); //Stored procedure that returns a selection of data
DataList1.DataSource = x.ToList();
DataList1.DataBind();

till here i get my data viewed correctly, I need to Update using my own stored procedure in this example from TextBox1 and TextBox2 to the Label1 ID 直到这里我可以正确查看数据,在此示例中,我需要使用自己的存储过程从TextBox1和TextBox2更新到Label1 ID

You can declare the button using the property CommandName and then use the ItemCommand event controller. 您可以使用属性CommandName声明按钮,然后使用ItemCommand事件控制器。

Source: http://msdn.microsoft.com/es-es/library/es4e4e0e(v=vs.100).aspx 来源: http : //msdn.microsoft.com/es-es/library/es4e4e0e(v=vs.100).aspx

Here, a working example (Note that I'm using an UpdatePanel . Due the content of the page is going to be change, if you do not use it you will get a server error. More info here ): 在这里,是一个有效的示例(请注意,我正在使用UpdatePanel 。由于页面的内容将要更改,如果不使用它,则会出现服务器错误。更多信息,请参见此处 ):

Test.aspx Test.aspx

<asp:UpdatePanel ID="upDataList1" runat="server" ChildrenAsTriggers="true">
  <ContentTemplate>       
    <asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
      <ItemTemplate>
        <table>
          <tr>
            <td>
              <asp:Label ID="lUID" runat="server" Text='<%# Eval("UID") %>' />
            </td>
            <td>
              <asp:TextBox ID="tbName" runat="server" Text='<%# Eval("name") %>' />
            </td>
            <td>
              <asp:TextBox ID="tbDescription" runat="server" Text='<%# Eval("description") %>' />
            </td>
            <td>
              <asp:Button ID="bModify" runat="server" Text="Modify" CommandName="Modify" />
            </td>
          </tr>
        </table>
      </ItemTemplate>
    </asp:DataList>
  </ContentTemplate>
</asp:UpdatePanel>

Test.aspx.cs Test.aspx.cs

public partial class Test : System.Web.UI.Page {
  protected void Page_Load(object sender, EventArgs e) {
    if (!Page.IsPostBack) {
      var x = db.sp_GetAllProducts();
      DataList1.DataSource = x;
      DataList1.DataBind();
    }
  }
  protected void DataList1_ItemCommand(Object sender, DataListCommandEventArgs e) {
    String a = ((TextBox) e.Item.FindControl("tbName")).Text;
    String b = ((TextBox) e.Item.FindControl("tbDescription")).Text;
    ((Label) e.Item.FindControl("lUID")).Text = a + " " + b;
  }
}

public class db {
  public String UID { get; set; }
  public String name { get; set; }
  public String description { get; set; }

  public db(String UID, String name, String description) {
    this.UID = UID;
    this.name = name;
    this.description = description;
  }
  public static List<db> sp_GetAllProducts() {
    List<db> list = new List<db>();
    list.Add(new db("1", "1a", "1b"));
    list.Add(new db("2", "2a", "2b"));
    list.Add(new db("3", "3a", "3b"));
    list.Add(new db("4", "4a", "4b"));
    list.Add(new db("5", "5a", "5b"));
    list.Add(new db("6", "6a", "6b"));
    return list;
  }
}

ADD OnItemCommand to your mark up 将OnItemCommand添加到您的标记中

<asp:DataList ID="DataList1" runat="server" OnItemCommand="Modify_ItemCommand" >

Then on Code Behind: 然后在后面的代码中:

protected void Modify_ItemCommand(object source, DataListCommandEventArgs e)
    {
 
        /* select the row index */
        int index = Convert.ToInt32(e.Item.ItemIndex);
 
        /*To get and Textbox of selected row*/
        TextBox txtbx = (TextBox)e.Item.FindControl("TextBox1");
 
        /* Assigning Value to your textbox */
        txtbx.Text = "What ever you want here";
 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM