无法从转发器内的文本框和下拉列表中获取任何数据

时间:2012-12-27 21:22:37

标签: c# asp.net asprepeater

您好我尝试从转发器(txtComment和ddlServiceStatus)中的控件中获取两个数据,因此我可以在btnNewComment中执行我的insert语句,但我无法获取任何数据。这是我的代码,请帮助我。

<asp:Repeater ID="Repeater1" runat="server"
        onitemdatabound="Repeater1_ItemDataBound">
    <ItemTemplate>
<table border="1" cellpadding="0" cellspacing="2" style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 13px; border-collapse: collapse; border-color:Black;"
        width="900px">
<tr style="color: #FFFFFF; background-color: #FF0000">
<td>Service Name</td> <td>Date</td><td>Channel Name</td><td>User Name</td><td>Msisdn</td><td>Status</td>
</tr>
<tr>
<td><asp:Label ID="lblService" runat="server" Text='<%# Eval("serviceName") %>'></asp:Label></td>
<td><asp:Label ID="lblDate" runat="server" Text='<%# Eval("date") %>'></asp:Label></td>
<td><asp:Label ID="lblChannelName" runat="server" Text='<%# Eval("channelName") %>'></asp:Label></td>
<td><asp:Label ID="lblUserName" runat="server" Text='<%# Eval("userName") %>'></asp:Label></td>
<td><asp:Label ID="lblMsisdn" runat="server" Text='<%# Eval("msisdn") %>'></asp:Label></td>
<td><asp:Label ID="lblStatus" runat="server" Text='<%# getStatusName(Eval("status")) %>'></asp:Label></td>   
</tr><tr><td colspan="6"><asp:Label ID="lblComment" runat="server" Text='<%# Eval("comment") %>' Width="900px"></asp:Label></td></tr>

<tr>
<td style="text-align:center;" colspan="6"><div><br /><a href="#" class="ToggleTikla" >
    <img src="../images/continuebtn.jpg" /> </a>

    <div id="fader"><br /> <asp:TextBox ID="txtComment" runat="server" TextMode="MultiLine" Width="900px" BorderStyle="Solid" BorderColor="Black" BackColor="#CCFFFF"></asp:TextBox><br />
        <asp:DropDownList ID="ddlServiceStatus" runat="server" ></asp:DropDownList>
       <asp:Button ID="btnNewComment" runat="server" Text="New Comment" ForeColor="White" BackColor="Red" Font-Bold="True" onclick="btnNewComment_Click" />     
        <br /><br />
        <asp:Repeater ID="rpNested" runat="server">
        <ItemTemplate>
        <table border="1" cellpadding="0" cellspacing="2" style="font-family: Arial, Helvetica, sans-serif; font-weight: bold; font-size: 13px; border-collapse: collapse; border-color:Black; text-align:left;" width="900px">

        <tr style="color: #FFFFFF; background-color: #FF0000">
        <td>Name</td>
        <td>Date</td>
        <td>Status</td>
        </tr>
        <tr>
        <td><asp:Label ID="lblExUserName" runat="server" Text='<%# Eval("Tester") %>'></asp:Label> </td>
         <td><asp:Label ID="lblExDate" runat="server" Text='<%# Eval("Date") %>'></asp:Label></td>
         <td><asp:Label ID="lblExstatus" runat="server" Text='<%# getStatusName(Eval("status")) %>'></asp:Label></td>    
        </tr>
        <tr><td colspan="3">
            <asp:Label ID="lblExCommand" runat="server" Text='<%# Eval("Comment") %>'></asp:Label></td></tr>
        </table> <br /><br />  
        </ItemTemplate>
        </asp:Repeater>
 </div>

</div>
</td>

 protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (txtAlert.Text.Length > 0)//if user entered an alertid
            {
                int alertID;
                try
                {
                    alertID = Convert.ToInt32(txtAlert.Text);//convert alertid to int alertid
                }
                catch (Exception ex)
                {
                    return;//do nothing user not entered a valid alertid
                }
            }
            SqlDataAdapter adp = new SqlDataAdapter("aaa_GetAlertbyUserID", _con);
            adp.SelectCommand.CommandType = CommandType.StoredProcedure;
            adp.SelectCommand.Parameters.AddWithValue("@userID", DropDownList1.SelectedValue);
            adp.SelectCommand.Parameters.AddWithValue("@alertID", txtAlert.Text);

            //create and show search results
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Repeater1.DataSource = ds;
            Repeater1.DataBind();
            //ds.Relations.Add(new DataRelation("alertComments",ds.Tables[0].Columns["ID"],ds.Tables[1].Columns["ID"]));
            //rpAlert.DataSource = ds;
            //rpAlert.DataBind();

            foreach (RepeaterItem ritem in Repeater1.Items)
            {
                DropDownList ddl = ritem.FindControl("ddlServiceStatus") as DropDownList;
                ddl = getAlertStatusList(ddl);
                TextBox txt = ritem.FindControl("txtComment") as TextBox;
                Button btn = ritem.FindControl("btnNewComment") as Button;
                //btn.Click += new EventHandler(btn_Click);
            }



        }

        private DropDownList getAlertStatusList(DropDownList ddl)
        {
            ddl.Items.Clear();
            SqlDataAdapter adp = new SqlDataAdapter("pr_GetAlertStatusList", _con);
            adp.SelectCommand.CommandType = CommandType.StoredProcedure;
            DataTable dt = new DataTable();
            adp.Fill(dt);
            ddl.DataSource = dt;
            ddl.DataValueField = "ID";
            ddl.DataTextField = "Name";
            ddl.DataBind();
            ddl.Items.Insert(0, "Please Select Status");
            return ddl;

        }

      protected void btnNewComment_Click(object sender, EventArgs e)
        {
            var status = Convert.ToInt32(((DropDownList)Repeater1.FindControl("ddlServiceStatus")).SelectedValue);
            var comment = ((TextBox)Repeater1.FindControl("txtComment")).Text;


            if (comment.Length == 0)
            {
                return;  
            }
            else
            {

                try
                {
                    int alertID = Convert.ToInt32(txtAlert.Text);
                    SqlDataAdapter adp = new SqlDataAdapter("aaa_InsertAlertCommentwithStatust", _con);  //
                    adp.SelectCommand.CommandType = CommandType.StoredProcedure;
                    adp.SelectCommand.Parameters.AddWithValue("@alertID", alertID);
                    adp.SelectCommand.Parameters.AddWithValue("@userID", DropDownList1.SelectedValue);
                    adp.SelectCommand.Parameters.AddWithValue("@date", DateTime.Now);
                    adp.SelectCommand.Parameters.AddWithValue("@comment", comment);
                    adp.SelectCommand.Parameters.AddWithValue("@status", status - 1);
                    DataSet ds = new DataSet();
                    adp.Fill(ds);
                    adp.SelectCommand.ExecuteNonQuery();
                }
                catch (Exception x)
                {

                    lblInfo.Text = "Error" + x.Message;
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

在大多数地方,您的开始和结束标记的格式不正确。您已经开始使用table代码asp:Repeater代码,但没有closing tag。所以首先改进这个领域。 现在来解决您的问题。您的第一个asp:Repeater没有closing tag因此,code behind您无法获取control的值。

以下是格式正确的html:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <table border="1" cellpadding="0" cellspacing="2" style="font-family: Arial, Helvetica, sans-serif;
            font-weight: bold; font-size: 13px; border-collapse: collapse; border-color: Black;"
            width="900px">
            <tr style="color: #FFFFFF; background-color: #FF0000">
                <td>
                    Service Name
                </td>
                <td>
                    Date
                </td>
                <td>
                    Channel Name
                </td>
                <td>
                    User Name
                </td>
                <td>
                    Msisdn
                </td>
                <td>
                    Status
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblService" runat="server" Text='<%# Eval("serviceName") %>'></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblDate" runat="server" Text='<%# Eval("date") %>'></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblChannelName" runat="server" Text='<%# Eval("channelName") %>'></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("userName") %>'></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblMsisdn" runat="server" Text='<%# Eval("msisdn") %>'></asp:Label>
                </td>
                <td>
                    <%--<asp:Label ID="lblStatus" runat="server" Text='<%# getStatusName(Eval("status")) %>'></asp:Label>--%>
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <asp:Label ID="lblComment" runat="server" Text='<%# Eval("comment") %>' Width="900px"></asp:Label>
                </td>
            </tr>
            <tr>
                <td style="text-align: center;" colspan="6">
                    <div>
                        <br />
                        <a href="#" class="ToggleTikla">
                            <img src="../images/continuebtn.jpg" />
                        </a>
                    </div>
                </td>
            </tr>
        </table>
        <div id="fader">
            <br />
            <asp:TextBox ID="txtComment" runat="server" TextMode="MultiLine" Width="900px" BorderStyle="Solid"
                BorderColor="Black" BackColor="#CCFFFF"></asp:TextBox><br />
            <asp:DropDownList ID="ddlServiceStatus" runat="server">
            </asp:DropDownList>
            <asp:Button ID="btnNewComment" runat="server" Text="New Comment" ForeColor="White"
                BackColor="Red" Font-Bold="True" OnClick="btnNewComment_Click" />
            <br />
            <br />
        </div>
    </ItemTemplate>
</asp:Repeater>
<asp:Repeater ID="rpNested" runat="server">
    <ItemTemplate>
        <table border="1" cellpadding="0" cellspacing="2" style="font-family: Arial, Helvetica, sans-serif;
            font-weight: bold; font-size: 13px; border-collapse: collapse; border-color: Black;
            text-align: left;" width="900px">
            <tr style="color: #FFFFFF; background-color: #FF0000">
                <td>
                    Name
                </td>
                <td>
                    Date
                </td>
                <td>
                    Status
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lblExUserName" runat="server" Text='<%# Eval("Tester") %>'></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblExDate" runat="server" Text='<%# Eval("Date") %>'></asp:Label>
                </td>
                <td>
                    <%--  <asp:Label ID="lblExstatus" runat="server" Text='<%# getStatusName(Eval("status")) %>'></asp:Label>--%>
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:Label ID="lblExCommand" runat="server" Text='<%# Eval("Comment") %>'></asp:Label>
                </td>
            </tr>
        </table>
        <br />
        <br />
    </ItemTemplate>
</asp:Repeater>