C# 如何从单击的链接按钮获取字符串?
我正在尝试做一个论坛,如果我按论坛主题,我会得到论坛id,并采取论坛id的会议,并显示在新的aspx。但是,当我尝试这样做时,问题是它无法识别ID=Label1(这是主题)。我认为这可能是因为它生成了多个Label1,因为它生成了所有要显示的论坛。以下是我的viewforum.aspx:C# 如何从单击的链接按钮获取字符串?,c#,asp.net,web,.net-framework-version,C#,Asp.net,Web,.net Framework Version,我正在尝试做一个论坛,如果我按论坛主题,我会得到论坛id,并采取论坛id的会议,并显示在新的aspx。但是,当我尝试这样做时,问题是它无法识别ID=Label1(这是主题)。我认为这可能是因为它生成了多个Label1,因为它生成了所有要显示的论坛。以下是我的viewforum.aspx: <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEven
<%@ 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>
$(文档).ready(函数(){
$(“.table”).prepend($(“”).append($(this.find)(.tr:first”)).dataTable();
});
函数readURL(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$('imgview').attr('src',e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
论坛
|
公司名称-
|出版商名称-
出版日期-
主持人姓名-
论坛介绍:
论坛状态:
这是我的viewforum.aspx的后端代码,它是viewforum.aspx.cs,我试图在这里获取已按下的论坛主题,但它无法识别ID标签1:
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)
{
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用系统配置;
使用系统数据;
使用System.Data.SqlClient;
使用System.IO;
名称空间讨论
{
公共部分类viewforum:System.Web.UI.Page
{
字符串strcon=ConfigurationManager.ConnectionString[“con”].ConnectionString;
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
尝试
{
SqlConnection con=新的SqlConnection(strcon);
if(con.State==ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd=new-SqlCommand(“选择*from forum_accept_tbl,其中forum_topic='”+Label1.Text.Trim(),con);
SqlDataReader dr=cmd.ExecuteReader();
如果(哈斯罗博士)
{
while(dr.Read())
{
写入(“警报('Login Successful');”;
会话[“论坛id”]=dr.GetValue(0).ToString();
重定向(“forumtaker.aspx”);
}
}
其他的
{
响应。写入(“警报(‘无效凭据’);”;
}
}
捕获(例外情况除外)
{
}
}
受保护的void GridView2\u SelectedIndexChanged(对象发送方,事件参数e)
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)
{
}
}