[英]How to add runat=server attribute to html buttons
I have multiple html buttons that generated from server side with a method on a csharp class. 我有多个html按钮,从服务器端生成一个csharp类的方法。
the result is: 结果是:
<button ID='btn1' runat='server' onserverclick='btn_Click'>Apply</button>
<button ID='btn2' runat='server' onserverclick='btn_Click'>Apply</button>
<button ID='btn3' runat='server' onserverclick='btn_Click'>Apply</button>
...
<button ID='btnN' runat='server' onserverclick='btn_Click'>Apply</button>
I also add btn_Click event to behind code: 我还将btn_Click事件添加到后面的代码中:
protected void btn_Click (object sender, EventArgs e)
{
string id = (sender as Control).ClientID;
msg = id;
}
as you can see I want to get buttonId but when I click on any of buttons, btn_Click won't call and I can't get buttonId. 你可以看到我想得到buttonId,但当我点击任何按钮时,btn_Click将不会调用,我无法获得buttonId。
I am using asp.net website with c#4.0. 我使用的是带有c#4.0的asp.net网站。
The problem is I need generate buttons using server side and runat attribute doesn't work with my method . 问题是我需要使用服务器端生成按钮,而runat属性不能与我的方法一起使用。 how can I fix this problem?
我该如何解决这个问题?
this is my method body for generated buttons 这是生成按钮的方法体
for (int i = 0; i < dt.Rows.Count; i++)
{
DateTime dtTemp = DateTime.Parse(dt.Rows[i]["messageDate"].ToString());
if (dt.Rows[i]["isRead"].ToString() == "True")
readed = "MessageReaded";
else
readed = "MessageNew";
post += "<div class='modal fade' id='myModal" + dt.Rows[i]["messageId"].ToString() + "' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>"
+ "<div class='modal-dialog' role='document'>"
+ "<div class='modal-content'>"
+ "<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'><span style='font-weight:bold'>subject</span> : " + dt.Rows[i]["subject"].ToString() + "</h4></div>"
+ "<div class='modal-header'><p><span style='font-weight:bold'>date</span> : " + dtTemp.ToString("yyyy/MM/dd") + "</p>"
+ "<p><span style='font-weight:bold'>Time</span> : " + dt.Rows[i]["messageTime"].ToString() + "</p>"
+ "<p><span style='font-weight:bold'>email</span> : " + dt.Rows[i]["email"].ToString() + "</p></div>"
+ "<div class='modal-body'>" + dt.Rows[i]["message"].ToString() + "</div>"
+ "<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>close</button>"
+ "<button ID='btn" + dt.Rows[i]["messageId"].ToString() + "' class='btn btn-danger' onclick='DeleteMessage_Click'>Delete</button></div>"
+ "</div></div></div>";
string narrow = Special.TimeToNarrow(dt.Rows[i]["messageDate"].ToString(), dt.Rows[i]["messageTime"].ToString());
post += "<a data-toggle='modal' data-target='#myModal" + dt.Rows[i]["messageId"].ToString() + "' href='#' class='list-group-item " + readed + "'><span class='badge'>" + narrow + "</span><i class='fa fa-fw fa-comment'></i> <span>"
+ dt.Rows[i]["name"].ToString() + "</span> : <span>" + dt.Rows[i]["subject"].ToString() + "</span></a>";
}
So the problem is on 所以问题就出现了
<button ID='btn" + dt.Rows[i]["messageId"].ToString() + "' runat='server' class='btn btn-danger' onserverclick='DeleteMessage_Click'>Delete</button>
EDIT MY POST : As you can see I can't use asp:button because using bootstrap modal. 编辑我的帖子:你可以看到我不能使用asp:按钮,因为使用bootstrap模式。 If you think that I can still use asp:button please write your code and show me how .thanks
如果您认为我仍然可以使用asp:button,请编写代码并告诉我如何。谢谢
There's no onserverclick
in asp. 在asp中没有
onserverclick
。 You should add the asp:Button
tag instead of just Button
and instead of onserverclick
just OnClick
您应该添加
asp:Button
标签而不仅仅是Button
而不是onserverclick
只是OnClick
Something like: 就像是:
<asp:Button ID='btn1' runat='server' Text="Apply" OnClick='btn_Click'/>
EDIT The button generation method should look like this: 编辑按钮生成方法应如下所示:
for (int i = 0; i < dt.Rows.Count; i++){
<div class='modal fade' id='myModal" + dt.Rows[i]["Id"].ToString() + "' tabindex='-1' role='dialog' aria-labelledby='myModalLabel'>
<div class='modal-dialog' role='document'>
<div class='modal-content'>
<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'><span style='font-weight:bold'>subject</span> :subject</h4></div>
<div class='modal-header'><p><span style='font-weight:bold'>Date</span> : yyyy/MM/dd</p>
<p><span style='font-weight:bold'>Time</span>messageTime</p>
<p><span style='font-weight:bold'>Email</span>email</p>
</div>
<div class='modal-body'>message</div>
<div class='modal-footer'>
<button type='button' class='btn btn-default' data-dismiss='modal'>close</button>
<asp:Button ID='btn" + dt.Rows[i]["messageId"].ToString() + "' runat='server' class='btn btn-danger' OnClick='DeleteMessage_Click'>Delete</button>
</div>
</div>
</div>
You can see that on this line: <button ID='btn" + dt.Rows[i]["messageId"].ToString() + "' runat='server' class='btn btn-danger' onserverclick='DeleteMessage_Click'>Delete</button>
i have changed the button
with asp:Button
and the onserverclick
with OnClick
. 你可以在这一行看到:
<button ID='btn" + dt.Rows[i]["messageId"].ToString() + "' runat='server' class='btn btn-danger' onserverclick='DeleteMessage_Click'>Delete</button>
我已经使用asp:Button
更改了button
,使用OnClick
更改了onserverclick
。
As other's have said, the problem is ASP.NET won't recognise runat="server"
when you generate your controls as a string of HTML. 正如其他人所说,问题是当您将控件生成为HTML字符串时,ASP.NET将无法识别
runat="server"
。
Your code should probably be done in a much different way, but if you wanted a change to what you have already done that will work you could do this a little differently. 你的代码应该以一种非常不同的方式完成,但是如果你想要改变你已经完成的工作,那么你可以做一些不同的事情。
Instead of runat="server"
on your buttons, make them submit buttons and assign them a name and value, for example... 而不是按钮上的
runat="server"
,让它们提交按钮并为它们分配名称和值,例如......
Instead of this: 而不是这个:
<button ID='btn1' runat='server' onserverclick='btn_Click'>Apply</button>
<button ID='btn2' runat='server' onserverclick='btn_Click'>Apply</button>
<button ID='btn3' runat='server' onserverclick='btn_Click'>Apply</button>
Do this (note they all have the same name): 这样做(注意它们都有相同的名字):
<button name="btn" type="submit" value="1">Apply</button>
<button name="btn" type="submit" value="2">Apply</button>
<button name="btn" type="submit" value="3">Apply</button>
Then in your Page_Load event you can detect if one of these buttons was clicked with the following: 然后在Page_Load事件中,您可以检测是否单击了其中一个按钮并显示以下内容:
if (Page.IsPostBack)
{
if (Request.Form["btn"] != null)
{
//A btn was clicked, get it's value
int btn = int.Parse(Request.Form["btn"]);
//Do something with this btn number
}
}
Here's a little sample that has three buttons and displays the number of the button that was clicked: 这是一个包含三个按钮的小样本,显示单击按钮的编号:
Test.aspx Test.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<button name="btn" type="submit" value="1">Apply</button>
<button name="btn" type="submit" value="2">Apply</button>
<button name="btn" type="submit" value="3">Apply</button>
</form>
</body>
</html>
test.aspx.cs test.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
if (Request.Form["btn"] != null)
{
int btn = int.Parse(Request.Form["btn"]);
Response.Write(btn);
}
}
}
}
The previous comments are correct, there is no onserverclick in asp.net server control. 以前的评论是正确的,在asp.net服务器控件中没有onserverclick。 onserverclick is available for html controls only.
onserverclick仅适用于html控件。 Below code piece does the job;
下面的代码片完成了工作;
protected void btn1_ServerClick(object sender, EventArgs e)
{
HtmlButton btn = (HtmlButton)sender;
btn.InnerText = btn.ID;
}
Cast the sender to HtmlButton to access the ID. 将发件人转发给HtmlButton以访问ID。 You have to use the namespace 'System.Web.UI.HtmlControls'.
您必须使用命名空间'System.Web.UI.HtmlControls'。
EDIT: 编辑:
If the buttons are being dynamically generated then better to use server button control; 如果按钮是动态生成的,那么最好使用服务器按钮控制;
<asp:Button ID="btnAddUser" runat="server" Text="Add" OnClick="btnAddUser_Click" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.