本文介绍了如何确定单击了哪个动态创建链接按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,

我的项目中有一个问题.有人可以帮我解决我的问题吗?如下所示.

我已经使用包含我的文件的文件夹名称动态创建了链接按钮,并为其分配了唯一的ID.
我还为他们提供了事件处理程序.

现在单击链接按钮,我想知道单击了哪个按钮,并将这些链接的完整路径存储在会话变量中,以便我可以访问下一页中存储的文本文件.

那么有人可以告诉我如何识别这些按钮吗?

该代码如下所示..预先表示感谢..


字符串stringChapterno =会话["chapterno"].ToString();
字符串stringAppApp = HttpContext.Current.Request.ApplicationPath;
字符串stringPhysicalPath = HttpContext.Current.Request.MapPath(stringAppPath);
stringPhysicalPath = stringPhysicalPath +"\\ App_Data \\" + stringChapterno;
//List< string> dirs = GetFilesRecursive(stringPhysicalPath);

foreach(Directory.GetDirectories(stringPhysicalPath)中的字符串p)
{
i = i + 1;
LinkBut​​ton lnkbtn =新的LinkBut​​ton();
ViewState ["btnlink" + i] = p.ToString();
字符串s = Path.GetFileNameWithoutExtension(p);
lnkbtn.Text = s;
lnkbtn.CssClass =超链接";
lnkbtn.ID ="btnlink" + i;
Panel1.Controls.Add(lnkbtn);
Panel1.Controls.Add(new LiteralControl(< br>")));
Panel1.Controls.Add(new LiteralControl(< br>")));
lnkbtn.Click + =新的EventHandler(this.lnkbtn_Click);
HiddenField1.Value = p;
base.OnInit(e);
}

}
受保护的void lnkbtn_Click(对象发送者,EventArgs e)
{
???
}

Hi friends,

I have one problem in my project.Can anybody help me to sort out my problem..It is as below..

I have dynamically created link buttons with the name of the folders which contain my files and assigned unique id to them.
I have also included the event handler for them.

Now on the click of the link button,I want to know which button was clicked and store in a session variable the complete path of those link sp that I can access the text files stored in them on the next page.

So can anybody tell me how to identify those buttons?

The code is given below..THanks in advance..


string stringChapterno = Session["chapterno"].ToString();
string stringAppPath = HttpContext.Current.Request.ApplicationPath;
string stringPhysicalPath = HttpContext.Current.Request.MapPath(stringAppPath);
stringPhysicalPath = stringPhysicalPath + "\\App_Data\\" + stringChapterno;
//List<string> dirs = GetFilesRecursive(stringPhysicalPath);

foreach (string p in Directory.GetDirectories(stringPhysicalPath))
{
i = i + 1;
LinkButton lnkbtn = new LinkButton();
ViewState["btnlink" + i] = p.ToString();
string s = Path.GetFileNameWithoutExtension(p);
lnkbtn.Text = s;
lnkbtn.CssClass = "hyperlink";
lnkbtn.ID = "btnlink" + i;
Panel1.Controls.Add(lnkbtn);
Panel1.Controls.Add(new LiteralControl("<br>"));
Panel1.Controls.Add(new LiteralControl("<br>"));
lnkbtn.Click += new EventHandler(this.lnkbtn_Click);
HiddenField1.Value = p;
base.OnInit(e);
}

}
protected void lnkbtn_Click(object sender, EventArgs e)
{
???
}

推荐答案

protected void lnkbtn_Click(object sender, EventArgs e)
{
    string id = ((LinkButton)sender).ID;
}


string ctrlname = page.Request.Params.Get("__EVENTTARGET");
if (ctrlname != null && ctrlname != string.Empty)
{
    return this.Page.FindControl(ctrlname);
}


它保存了引起回发的控件的UniqueID.


It holds the UniqueID of the control that caused postback.


这篇关于如何确定单击了哪个动态创建链接按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 04:27