开发者

Control within another control in asp.net disables server-side events?

I have a control which has a button on it. All the button does is redirect the user to another page. On this control I have another control which just display some stuff, nothing fancy. When I click on the button, it does nothing. When I remove or comment out the other control, the button event works. Is this by design? I am not sure how to fix it or what is causing it.

ControlA.ascx contains a button with an event like so:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Home.aspx");
}

ControlA.ascx contains another control inside of it, lets call it ControlB.ascx.

The Button1 click event does not fire when ControlB.ascx is in ControlA.ascx, but when I remove it or take it out, Button1 event fires and it goes to Home.aspx. This does not only happen for Button1, if I decide to add a second button and create an event for it, if ControlB is in ControlA, the second button event won't fire either.

ControlA.asx markup and code:

<div>
    <asp:Button runat="server" ID="Button1" Text="Register 2" 
                           onclick="Button1_Click" />
</div>
<div>
    <uc:ControlB ID="ControlB" runat="server" />
</div>

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("~/Home.aspx");
}

ControlB.ascx markup:

<div>
  <p>I am control B</p>
  <p>
     <sbi:SexyLinkButton ID="SexyLinkButton1" runat="server" Text="Go Home" 
                         PostBackUrl="~/home.aspx" />

      <asp:Button runat="server" ID="Button1" Text="Register 2" 
                           onclick="Button1_Click" />

</p>
</div>

As stated before, Button1_Click does not fire if ControlB is inside ControlA, but if I remove ControlB or comment it out, Button1_Click fires.

I figured something out:

ControlB.ascx had a button on it called SexyButton ( didn't create this, someone else did, lol) and when I removed that from ControlB, ControlA's button worked. I put an asp.net Button in controlB and it worked, so it has something to do wit开发者_Python百科h the SexyButton control.

If I put an asp.net button on ControB along with the SexyLinkButton, the event for Button1 on ControlB does not work.

SexyLinkButton.cs - If I put the PostBackUrl on the SexyLinkButton, asp.net button events don't fire. Here is the code the SexyLinkButton.cs:

public class SexyLinkButton : LinkButton
{
    private List<string> Css = new List<string> ();

    public string IconCssClass { get; set; }
    public SizeEnum Size { get; set; }

    public SexyLinkButton()
    {
        Size = SizeEnum.Normal;
    }

    protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        if (!string.IsNullOrEmpty(CssClass)) Css.Add(CssClass);             // User added Css
        Css.Add("sbi-button");                                              // Standard Css for button
        if (Size != SizeEnum.Normal) Css.Add(Size.ToString().ToLower());    // Size Css

        // Nested spans per design and css requirements
        HtmlGenericControl span1 = new HtmlGenericControl("span");
        HtmlGenericControl span2 = new HtmlGenericControl("span");

        // Nested span only for icon
        if (!string.IsNullOrEmpty(this.IconCssClass))
        {
            HtmlGenericControl btnspan = new HtmlGenericControl("span");
            btnspan.Attributes.Add("class", IconCssClass.Replace(".", string.Empty));
            btnspan.InnerHtml = this.Text;
            span2.Controls.Add(btnspan);
        }
        else
        {
            span2.InnerHtml = this.Text;
        }

        CssClass = string.Join(" ",Css.ToArray());
        this.Text = "";
        span1.Controls.Add(span2);
        this.Controls.Add(span1);

        base.Render(writer);
    }

    public enum SizeEnum
    {
        Normal,
        Medium,
        Large
    }
}


What's probably happening is that your "SexyLinkButton" control is causing a javascript error that's preventing your button's postback. Check your browser's error console to see if any javascript errors are being generated. Also, try substituting a standard LinkButton control instead of the SexyLinkButton to see if that fixes it.

Edit: looks like it's actually a form issue with the PostBackUrl.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜