[英]Why does my function not work?
I use Ajax toolkit.I want create text editor for HTML codes. 我使用Ajax工具包。我想为HTML代码创建文本编辑器。 I found .css file in html link tags and for each of these files, create a LinkButton. 我在html链接标记中找到了.css文件,并为每个文件创建一个LinkButton。 I want when user click linkbutton .css file open in editor. 我想要当用户单击链接按钮.css文件在编辑器中打开时。 I write this code: 我写这段代码:
string textHtml = "";
List<string> listTextHtml = new List<string>();
protected void Page_Load(object sender, EventArgs e)
{
AsyncPostBackTrigger d = new AsyncPostBackTrigger();
if (IsPostBack)
return;
StreamReader re = new StreamReader(string.Concat(Server.MapPath("/"), "\\Engine\\TextFile1.txt"));
while (re.Peek() > 0)
{
txtHtmlCode.Content = txtHtmlCode.Content + re.ReadLine() + "\n";
}
textHtml = txtHtmlCode.Content;
re.Close();
string temp = textHtml;
while (temp.Contains("link"))
{
string s = temp.Substring(temp.IndexOf("link"), temp.IndexOf("/>"));
temp = temp.Substring(temp.IndexOf("/>") + 2);
if (s.IndexOf(".css") >= 0)
{
s = s.Substring(s.IndexOf("href=\"") + 6);
listTextHtml.Add(s.Substring(0, s.IndexOf("\"") - 1));
}
//temp = temp.Substring(temp.IndexOf("href="));
//temp = temp.Substring(temp.IndexOf("\"") + 1);
//listTextHtml.Add(temp.Substring(0, temp.IndexOf("\"") - 1));
//temp = temp.Substring(temp.IndexOf("\"") + 1);
}
int i = 0;
hfString.Value = "";
foreach (var item in listTextHtml)
{
//HtmlGenericControl li = new HtmlGenericControl("li");
LinkButton lb = new LinkButton();
lb.ID = i.ToString();
lb.Text = "Link - " + i.ToString();
//lb.Click += ltnCssLoad_Click;
lb.Click += new EventHandler(this.ltnCssLoad_Click);
//li.Controls.Add(lb);
phTags.Controls.Add(lb);
d.ControlID = i.ToString();
d.EventName = "Click";
//udpMain.Triggers.Add(d);
i++;
hfString.Value += item + "|";
}
}
when click the linkbutton
this function must run: 当单击linkbutton
此功能必须运行:
protected void ltnCssLoad_Click(object sender, EventArgs e)
{
LinkButton ClickedLink = (LinkButton)sender;
string[] listText = hfString.Value.Split('|');//string.Concat(Server.MapPath("/"),
StreamReader re = new StreamReader(string.Concat(string.Concat(Server.MapPath("/"), listText[int.Parse(ClickedLink.ID)])));
txtHtmlCode.Content = "";
while (re.Peek() > 0)
{
txtHtmlCode.Content = txtHtmlCode.Content + re.ReadLine() + "\n";
}
re.Close();
}
but when I use break point in function ltnCssLoad_Click
, this function not work when I click in linkbutton. 但是当我在功能ltnCssLoad_Click
使用断点时,当我在linkbutton中单击时,此功能不起作用。 How can I fix this? 我怎样才能解决这个问题?
This is not working because when you click link button or any dynamically added control it causes post back and you have "return"ed in page load event handler if IsPostBack is true. 这是行不通的,因为当您单击链接按钮或任何动态添加的控件时,它会导致回发,并且如果IsPostBack为true,则会在页面加载事件处理程序中“返回”。
The pain with using dynamic controls is that you have to recreate them every time (first request as well as subsequent post backs). 使用动态控件的痛苦在于您必须每次都重新创建它们(第一个请求以及随后的回发)。
Restrict file reading to first request (ie put it in !IsPostBack condition) and let link buttons' creation code run after that. 将文件读取限制为第一个请求(即,将其置于!IsPostBack条件中),然后让链接按钮的创建代码运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.