3 个链接按钮 1 个事件处理程序
我是asp.net的初学者,我已经做了我的研究,但不是很清楚。
我有 3 个链接 lkn1,2,3
基本上,我正在看这样的内容:
protected void lnkBtn_Click(object sender,EventArgs e)
{
LinkButton lnkRes = sender as LinkButton;
string text = lnkRes.Text.Trim();
string sql = ""
if(text.ToUpper() == "INBOX")
{
sql = "SELECT * FROM InboxTbl where receiver_id = "helloworld";
}
else if(text.ToUpper() == "DRAFT")
{
sql = "SELECT * FROM Inbox where sender_id="HelloWorld";
}
else if(text.ToUpper() == "SENT")
{
sql = "SELECT * FROM Inbox where sender_id="HelloWorld";
}
if(sql != "")
{
SqlDataAdapter adp = new SqlDataAdapter(sql,ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables["tbl"].DefaultView;
GridView1.DataBind();
}
}
如何编写此代码以及应该在哪里编写它,以便根据链接按钮的文本执行相应的 sql 语句?
如果这与事件处理有关.. 3links one event.. 请给我发送一些我可以阅读和理解的链接
I am a beginner in asp.net, I've done my research but not very clear.
I have 3 links lkn1,2,3
Basically, I am looking at something like this:
protected void lnkBtn_Click(object sender,EventArgs e)
{
LinkButton lnkRes = sender as LinkButton;
string text = lnkRes.Text.Trim();
string sql = ""
if(text.ToUpper() == "INBOX")
{
sql = "SELECT * FROM InboxTbl where receiver_id = "helloworld";
}
else if(text.ToUpper() == "DRAFT")
{
sql = "SELECT * FROM Inbox where sender_id="HelloWorld";
}
else if(text.ToUpper() == "SENT")
{
sql = "SELECT * FROM Inbox where sender_id="HelloWorld";
}
if(sql != "")
{
SqlDataAdapter adp = new SqlDataAdapter(sql,ConnectionString);
DataSet ds = new DataSet();
adp.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables["tbl"].DefaultView;
GridView1.DataBind();
}
}
How do I write this code and where should I write it, so that depending on the text of the linkbutton, the respective sql statement is executed?
If this has anything to do with event handling.. 3links one event.. pls send me some links I could read and understand
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
此处是描述添加事件的链接处理程序。通过事件处理,当某个操作发生时,响应是调用某个函数(在我们的例子中
lnkBtn_Click
)。为了让 3 个不同的 LinkButton 使用相同的函数,我们只需将相同的函数放在 OnClick 属性上 - 这使得所有三个 LinkButton 的事件处理程序具有相同的函数。Here is a link describing add event handlers. With event handling, when a certain action occurs, the response is to call a certain function (in our case
lnkBtn_Click
). For 3 different LinkButtons to use the same function, we just put the same function on the OnClick attribute - this is what makes the event handler for all three LinkButtons the same function.在 HTML 中写入以下内容
in the HTML write the below
不使用文本属性,而是使用源来查找单击了哪个按钮。其余的一切对我来说看起来都很好。
Instead of using the text property use the source to find which button is clicked. Rest all looks fine to me.