如何在ASP.NET中的中继器控件中使用控件



这是我的C#代码:

    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;&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Template>
    <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>

我想在中继器控件内使用控件的Text属性,并通过按钮单击事件将文本值插入表中,但我无法访问控件本身。它显示出这样的错误:

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

我该如何解决?

预先感谢。

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

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

最新更新