I'm trying to do a forum where if I press on the forum topic, I will get the forum id and take the session of the forum id and display it on new aspx. However, The problem when I try to do this is that it doesn't recognize the ID = Label1 ( which is the topic ). I'm thinking that this maybe because it generates multiple Label1 as it generates all forums to be shown. This below is my viewforum.aspx:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="viewforum.aspx.cs" Inherits="ForumDiscussion.viewforum" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
$(document).ready(function () {
$(".table").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable();
});
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#imgview').attr('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="container-fluid">
<div class="row">
<div class="col">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col">
<center>
<h4>Forum</h4>
</center>
</div>
</div>
<div class="row">
<div class="col">
<hr>
</div>
</div>
<div class="row">
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:elibraryDBConnectionString %>" SelectCommand="SELECT [forum_id], [forum_topic], [forum_description], [forum_status], [company_name], [replies_count], [publish_date], [publisher_name], [forum_img_link], [moderator_name] FROM [forum_accept_tbl]"></asp:SqlDataSource>
<div class="col">
<asp:GridView class="table table-striped table-bordered" ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="forum_id" HeaderText="ID" ReadOnly="True" SortExpression="forum_id" InsertVisible="False" >
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<div class ="container-fluid">
<div class ="row">
<div class ="col-lg-8">
<div class="row">
<div class="col-19">
<asp:LinkButton ID="Label1" runat="server" Text='<%# Eval("forum_topic") %>' Font-Bold="True" Font-Size="X-Large" Font-Underline="true" OnClick="Button1_Click"></asp:LinkButton>
|
</div>
</div>
<div class="row">
<div class="col-12">
<span>Company Name - </span>
<asp:Label ID="Label2" runat="server" Font-Bold="True" Text='<%# Eval("company_name") %>'></asp:Label>
| <span><span> </span> Publisher Name - </span>
<asp:Label ID="Label3" runat="server" Font-Bold="True" Text='<%# Eval("publisher_name") %>'></asp:Label>
</div>
</div>
<div class="row">
<div class="col-12">
<span>Publish Date - </span>
<asp:Label ID="Label9" runat="server" Font-Bold="True" Text='<%# Eval("publish_date") %>'></asp:Label>
</div>
</div>
<div class="row">
<div class="col-12">
<span>Moderator Name - </span>
<asp:Label ID="Label5" runat="server" Font-Bold="True" Text='<%# Eval("moderator_name") %>'></asp:Label>
</div>
</div>
<br />
<div class="row">
<div class="col-12">
Forum Description :
<br />
<asp:Label ID="Label12" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Small" Text='<%# Eval("forum_description") %>'></asp:Label>
</div>
</div>
</div>
<div class ="col-lg-2">
Forum Status:
<asp:Label ID="Label4" runat="server" Text='<%# Eval("forum_status") %>' Font-Bold="True" Font-Size="X-Large" Font-Underline="true"></asp:Label>
</div>
<div class ="col-lg-2">
<asp:Image CssClass="img-fluid" ID="Image1" Height="200px" Width="200px" background-size="cover" background-repeat="no-repeat" background-postion="50% 50%" runat="server" ImageUrl='<%# Eval("forum_img_link") %>' />
</div>
</div>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</asp:Content>
And here is my back end code of my viewforum.aspx which is the viewforum.aspx.cs where I try to take the pressed forum topic but it doesn't recognize the ID label1:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace ForumDiscussion
{
public partial class viewforum : System.Web.UI.Page
{
string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(strcon);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select * from forum_accept_tbl where forum_topic='" + Label1.Text.Trim(), con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Response.Write("<script>alert('Login Successful');</script>");
Session["forum_id"] = dr.GetValue(0).ToString();
Response.Redirect("forumtaker.aspx");
}
}
else
{
Response.Write("<script>alert('Invalid credentials');</script>");
}
}
catch (Exception ex)
{
}
}
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
Is there any other way to click the forum_topic and take it as a string so I can take the forum id?
In Button1_Click, you need to get the Linkbutton like this
LinkButton lnkButton= (LinkButton)sender;
and then you can get the Link Button text
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//Get the Link button
LinkButton lnkButton= (LinkButton)sender;
SqlConnection con = new SqlConnection(strcon);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("select * from forum_accept_tbl where forum_topic='" + lnkButton.Text.Trim(), con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Response.Write("<script>alert('Login Successful');</script>");
Session["forum_id"] = dr.GetValue(0).ToString();
Response.Redirect("forumtaker.aspx");
}
}
else
{
Response.Write("<script>alert('Invalid credentials');</script>");
}
}
catch (Exception ex)
{
}
}