从转发器内的标签中获取id的值

时间:2011-09-13 18:25:57

标签: c# asp.net dynamic repeater sqldatasource

嘿,我现在有点卡住, 我正在创建一个论坛,我似乎无法在后面的代码中获得“threadID”,我需要这个功能,因为我然后用它来发布到数据库。

“threadID”是在第一个转发器中动态生成的,我需要获取该值。

目前我在浏览器中收到此错误:

“编译器错误消息:CS0103:当前上下文中不存在名称'threadID'”

这是代码:

<h1>viewThread.aspx</h1>
    <!-- THREAD TEXT STARTS --->
    <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" EnableViewState="False">
        <ItemTemplate>
            <h2><%# Eval ("threadTitle") %></h2>
            <!--- THIS THREAD ID NEEDS TO BE GENERATED DYNAMICALLY SO WE CAN THEN GRAB IT TO USE IN THE REPLY --->
            <asp:Label ID="threadID" runat="server" Visible="false" Text='<%# Eval ("threadID") %>' />
            <%# Eval ("threadText") %>
        </ItemTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:VSTESTConnectionString %>" 
        SelectCommand="SELECT * FROM [threadCat&amp;Thread] WHERE ([threadID] = @threadID)">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="1" Name="threadID" QueryStringField="t" 
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <!-- THREAD TEXT ENDS --->

    <!-- USER GENERATED REPLYS --->
    <h2>Replys</h2>

    <asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
        <ItemTemplate>
            <%#Eval ("postText") %> <br />
           Date Posted <%#Eval ("postCreated") %>
        </ItemTemplate>
    </asp:Repeater>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
        ConnectionString="<%$ ConnectionStrings:VSTESTConnectionString %>" 
        SelectCommand="SELECT * FROM [posts] WHERE ([threadID] = @threadID)">
        <SelectParameters>
            <asp:QueryStringParameter DefaultValue="1" Name="threadID" QueryStringField="t" 
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>
    <!-- USER GENERATED REPLYS ENDS --->

    <!-- REPLY --->
    <h2>post a reply</h2>
    Userid<asp:TextBox ID="userID" runat="server" /><br />
    <asp:TextBox ID="postTxt" TextMode="MultiLine" runat="server"></asp:TextBox><br />
    <asp:Button ID="reply" runat="server" Text="Reply" onclick="reply_Click" />
    <!-- REPLY ENDS --->
</asp:Content>

<h1>viewThread.aspx.cs</h1>



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class viewThread : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void reply_Click(object sender, EventArgs e)
    {
        /* threadID is not showing in intellisense */
        string thread = threadID.Text;
        string user = userID.Text;
        string reply = postTxt.Text;
        string date = DateTime.Now.ToString();
        int locked = 0;

        SqlConnection sqlCon = new SqlConnection("Server =.\\SQLEXPRESS; Database =VSTEST; Trusted_Connection =True;");
        SqlCommand sqlCom = new SqlCommand("INSERT INTO posts(threadID, postBy, postText, postCreated, postLocked) VALUES ('" + thread + "','" + user + "','" + reply + "','" + date + "','" + locked + "' )", sqlCon);
        try
        {
            sqlCon.Open();
            sqlCom.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write("Brokies.... " + ex.ToString());
        }
        finally
        {
            sqlCon.Close();
            Response.Redirect("/forums/Default.aspx");
        }
    }
}

使用viewstate解决了问题,更改如下:

public partial class viewThread : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["t"] != null && Request.QueryString["t"].ToString() != "")
        {
            ViewState["threadID"] = Request.QueryString["t"].ToString();
        }
    }

    protected void reply_Click(object sender, EventArgs e)
    {
        string user = userID.Text;
        string reply = postTxt.Text;
        string date = DateTime.Now.ToString();
        int locked = 0;

        SqlConnection sqlCon = new SqlConnection("Server =.\\SQLEXPRESS; Database =VSTEST; Trusted_Connection =True;");
        SqlCommand sqlCom = new SqlCommand("INSERT INTO posts(threadID, postBy, postText, postCreated, postLocked) VALUES ('" + ViewState["threadID"].ToString() + "','" + user + "','" + reply + "','" + date + "','" + locked + "' )", sqlCon);
        try
        {
            sqlCon.Open();
            sqlCom.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Response.Write("Brokies.... " + ex.ToString());
        }
        finally
        {
            sqlCon.Close();
            Response.Redirect("/forums/Default.aspx");
        }
    }

}

1 个答案:

答案 0 :(得分:0)

尝试使用CSS设置可见性,而不是设置Visible="false"

<asp:Label ID="threadID" runat="server" style="display:none;" Text='<%# Eval("threadID") %>' />

然后尝试使用此代码:

Label lbl = Repeater1.Items[0].FindControl("threadID") as Label;
if (lbl != null)
{
    int threadID = Int32.Parse(lbl.Text);
}

老实说,我建议改用ListView,这样就可以定义数据键了。这样会更容易:

<asp:ListView ID="ListView1" runat="server" DataKeyNames="threadID">
    <ItemTemplate> 
        <h2><%# Eval ("threadTitle") %></h2> 
        <%# Eval ("threadText") %> 
    </ItemTemplate>     
</asp:ListView>

然后您可以在后面的代码中访问threadID这样的代码:

int itemIndex = 0; //index of your current item
int threadID = (int)ListView1.DataKeys[itemIndex]["threadID"];