Grab id's value from a label inside a repeater
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 GENERATE开发者_开发百科D 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");
}
}
}
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"];
精彩评论