简体   繁体   English

为什么我的功能不起作用?

[英]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文件,并为每个文件创建一个LinkBut​​ton。 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.

相关问题 为什么某些 CSS Function 在我的 WPF WebBrowser 元素中不起作用? - Why does some CSS Function not work in my WPF WebBrowser Element? WPF- 为什么我的项目中的 keyDown 功能不起作用? - WPF- Why keyDown function in my project does not work? 为什么我的排序不起作用? - Why does my sorting not work? 为什么这个 IEnumerator function 不起作用 - Why does this IEnumerator function not work 为什么 cURL 有效,但我的 HttpWebRequest 无效? - Why does cURL work, but my HttpWebRequest does not? 为什么我的Javascript函数在通过`onclick`调用时有效,而在以编程方式调用时却不起作用? - Why does my Javascript function work when called with `onclick` but not when called programmatically? 为什么我的 C# 函数返回字母表中字母的位置? - Why does my C# function returning the position of a letter in the alphabet work? 为什么我的 javascript 函数只能从代码隐藏调用一次? - Why does my javascript function only work once calling from codebehind? 在SQL Server Management Studio中修改表值函数时,为什么我的表值函数工作得更快? - Why does my table value function work faster, when i modify it in SQL server management studio? 为什么这个功能在我的主机上运行良好而在虚拟机上运行不正常? (GetPhysicallyInstalledSystemMemory) - Why does this function work well on my main machine but not on a virtual one? (GetPhysicallyInstalledSystemMemory)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM