从中继器内的标签中获取 id 值

发布于 2024-12-04 22:18:09 字数 5183 浏览 3 评论 0原文

嘿,我现在有点陷入困境, 我正在创建一个论坛,我似乎无法在后面的代码中获取“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");
        }
    }
}

已通过使用视图状态解决了问题,更改如下:

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

}

Hey I am a bit stuck at the moment,
I am creating a forum, I cant seem to get the "threadID" in the code behind, I need this to function as I then use it to post to the database.

The "threadID" is being dynamically generated in the first repeater, I need to way to grab that value.

at the moment I am getting this error in the browser:

"Compiler Error Message: CS0103: The name 'threadID' does not exist in the current context"

here's the code:

<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&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");
        }
    }
}

Have solved issue by using the viewstate, changes are below:

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-12-11 22:18:09

尝试使用 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"];

Try setting the visibility with CSS instead of setting Visible="false".

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

And then try to use this code:

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

Honestly, I would suggest using a ListView instead, so you can define data keys. This will be much easier that way:

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

And then you can access the threadID like this in the code behind:

int itemIndex = 0; //index of your current item
int threadID = (int)ListView1.DataKeys[itemIndex]["threadID"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文