0

I write one example to create own control on ASP.NET Froms. The controls very simple- combobox and button. User need choose value and when after he submit the button, the value from combobox need display in label. So. Code of my Control:

public class MyControl:Control,IPostBackEventHandler
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.AddAttribute("size","1");
        writer.AddAttribute("ID","List2");
        writer.AddAttribute("name", "ListYear");
        writer.RenderBeginTag(HtmlTextWriterTag.Select);

        for (int i = 1950; i < DateTime.Now.Year; i++)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Option);
            writer.WriteEncodedText(i.ToString());
            writer.RenderEndTag();
        }

        writer.RenderEndTag();

        writer.AddAttribute("type","submit");
        writer.AddAttribute("value","ClickMe");
        writer.AddAttribute("name","BtnChange");
        writer.RenderBeginTag(HtmlTextWriterTag.Input);
        writer.RenderEndTag();

        base.Render(writer);
    }

    public delegate void OnClickEventHandler(object sender, EventArgs args);

    public event OnClickEventHandler Click;

    public void RaisePostBackEvent(string eventArgument)
    {
        Click(this, new EventArgs());
    }
}

The Page ASP:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestMyControl.aspx.cs" Inherits="Hello.TestMyControl" %>
<%@ Register assembly="Hello" namespace="Hello" tagPrefix="MyContrl" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
        <br />
    <MyContrl:MyControl runat="server" OnClick="Unnamed1_OnClick" ID="Control1"></MyContrl:MyControl>
    </div>
    </form>
</body>
</html>

And in the end Event function:

protected void Unnamed1_OnClick(object sender, EventArgs args)
    {
        Label1.Visible = true;
        Label1.Text="You choose "+Control1.????+" year";
    }

What substitute for a question mark that take the value from the list?

P.S. Something strange is going on. Because when I click the button, the handler is not called, and I can not get into Unnamed1_OnClick

4

3 回答 3

1

由于您已在属性上设置了值,因此要检索它,您需要访问Attributes属性

使您的控件继承自HtmlControl

public class MyControl : HtmlControl, IPostBackEventHandler
{
    ...

在您的页面上

<MyContrl:MyControl runat="server" OnClick="Unnamed1_OnClick" ID="Control1"></MyContrl:MyControl>

在你的代码上

Label1.Text = Control1.Attributes["value"];

您可以调试此行以查看所有可用属性

于 2013-06-03T19:32:03.387 回答
0

您需要传递 的名称combobox及其所选文本的值。

像这样:

protected void Unnamed1_OnClick(object sender, EventArgs args)
{
    Label1.Visible = true;
    Label1.Text="You choose "+ myCustomControl.SelectedItem.Value.ToString() 
    + " year";
}

(对不起。我误读了最初的帖子,并在我意识到自己的错误后相应地编辑了我的代码。)

于 2013-06-03T19:29:59.533 回答
0

使用 name="YourSelectList" 将选择列表添加到您的用户控件

然后在点击事件处理程序中

protected void Unnamed1_OnClick(object sender, EventArgs args)
    {
        Label1.Visible = true;
        Label1.Text="You choose "+Control1.YourSelectList.SelectedValue.ToString()+" year";
    }
于 2013-06-03T19:31:41.510 回答