如何在ASP.NET中使用转发器控件内的控件?

时间:2017-02-17 15:02:21

标签: c# asp.net sql-server-2008 insert repeater

这是我的C#代码:

InterfaceMethodref_info

我想在Repeater控件中使用控件的 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { int id; protected void Page_Load(object sender, EventArgs e) { Label2.Text = " " + Session["username"] + " "; string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(CS)) { id = Convert.ToInt32(Request.QueryString["Prod_Id"].ToString()); con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from Product where Prod_Id="+ id +""; cmd.ExecuteNonQuery(); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); Repeater1.DataSource = dt; Repeater1.DataBind(); con.Close(); } } protected void Button1_Click(object sender, EventArgs e) { string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; using (SqlConnection con = new SqlConnection(CS)) { con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "insert into Order values('" + Label2.Text + "','"+ Label3.Text +"')"; cmd.ExecuteNonQuery(); con.Close(); } } } here is my.aspx code <%@ Page Title="" Language="C#" MasterPageFile="~/welcome_mstr.master" AutoEventWireup="true" CodeFile="desc.aspx.cs" Inherits="_Default" %> <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Welcome</b> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> &nbsp;!<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand"> <HeaderTemplate></HeaderTemplate> <ItemTemplate> <div style=" width:700px; margin-left:200px; background-color:#FCFBE3"> <br /> <table> <tr><td><img alt="" src='<%# Eval("Image") %>' style=" width:299px; height:299px;" /></td> <td valign="top"><table > <tr><td align="left"><asp:Label ID="Label3" runat="server" Text='<%#Eval("Prod_Name") %>'></asp:Label></td></tr> <tr><td align="left"><asp:Label ID="Label4" runat="server" Text='<%#Eval("Weight") %>'></asp:Label></td></tr> <tr><td align="left">Cost: ₹<asp:Label ID="Label1" runat="server" Text='<%#Eval("Price") %>'></asp:Label></td></tr> <tr><td> <asp:Button ID="Button1" runat="server" Text="Order" /> </td></tr> </table> </td> </tr> </table> </div> </ItemTemplate> <FooterTemplate></FooterTemplate> </asp:Repeater> </asp:Content> 属性,并通过按钮单击事件将该文本值插入表中,但我无法访问控件本身。它显示如下错误:

  

当前上下文中不存在名称“label3”

我怎么解决这个问题?

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以像以下代码段一样访问它:

protected void Button1_Click(object sender, EventArgs e)
{
    string strLabel3 = string.Empty;
    foreach (RepeaterItem ri in Repeater1.Items)
    {
        Label lbl3= (Label)ri.FindControl("Label3");
        strLabel3 = lbl3.Text;
    }

    //Remaing logic
}

更新1

你也可以这样做:

protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button) sender;
    RepeaterItem item = (RepeaterItem) btn.NamingContainer;
    Label lbl3 = (Label)item.FindControl("Label3");
    string strlbl3 = lbl3.Text;
}