从中继器内的标签中获取 id 值
嘿,我现在有点陷入困境, 我正在创建一个论坛,我似乎无法在后面的代码中获取“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&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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 CSS 设置可见性,而不是设置
Visible="false"
。然后尝试使用此代码:
老实说,我建议改用 ListView,这样您就可以定义数据键。这样会容易得多:
然后您可以在后面的代码中像这样访问
threadID
:Try setting the visibility with CSS instead of setting
Visible="false"
.And then try to use this code:
Honestly, I would suggest using a ListView instead, so you can define data keys. This will be much easier that way:
And then you can access the
threadID
like this in the code behind: