为什么用户控件内的按钮的OnClick事件没有发生

时间:2012-04-13 13:55:02

标签: c# asp.net user-controls

.NET专业人士

我有一个问题,我想与您分享,如果可能的话,请提出建议:

我正在使用datalist动态加载usercontrol。我正在为从数据库中获取的每一行加载此usercontrol,它可能是此控件的30个不同的实例。

当用户单击按钮时,我希望能够获取此特定usercontrol属性的值,在我的理解中,我注意到usercontrol ID来执行此操作,这就是为什么我要将“自定义id”分配给每个加载的控件,以便我以后能够找到它。 也许我错过了一些东西,也许这不是最好的方式,我愿意接受建议。 无论如何.....

后面的UserControl代码:

public partial class SearchBullet : System.Web.UI.UserControl
{
public static int i = 0;
private string casterLOGO;

public string CasterLOGO
{
    get { return casterLOGO; }
    set { casterLOGO = value; }
}
private string casterNameSB;

public string CasterNameSB
{
    get { return casterNameSB; }
    set { casterNameSB = value; }
}
private string player1NameSB;

public string Player1NameSB
{
    get { return player1NameSB; }
    set { player1NameSB = value; }
}
private string player2NameSB;

public string Player2NameSB
{
    get { return player2NameSB; }
    set { player2NameSB = value; }
}
private string mapSB;

public string MapSB
{
    get { return mapSB; }
    set { mapSB = value; }
}
private string gameFrameSB;

public string GameFrameSB
{
    get { return gameFrameSB; }
    set { gameFrameSB = value; }
}
private string serieSB;

public string SerieSB
{
    get { return serieSB; }
    set { serieSB = value; }
}
private string race1SB;

public string Race1SB
{
    get { return race1SB; }
    set { race1SB = value; }
}
private string race2SB;

public string Race2SB
{
    get { return race2SB; }
    set { race2SB = value; }
}
private string castURLSB;

public string CastURLSB
{
    get { return castURLSB; }
    set { castURLSB = value; }
}
private string likeAmountSB;

public string LikeAmountSB
{
    get { return likeAmountSB; }
    set { likeAmountSB = value; }
}
private string qRYstring;

public string QRYstring
{
    get { return qRYstring; }
    set { qRYstring = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack == false)
    {
        i++;
        this.PreRender += new EventHandler(Cast_PreRender);

        this.ID = "Result" + i.ToString();  


    }
    PlayButton.ID = i.ToString();

}

void Cast_PreRender(object sender, EventArgs e)
{
    PlayerName1.Text = Player1NameSB;
    PlayerName2.Text = Player2NameSB;
    CasterName.Text = CasterNameSB;
    ImageRace1.ImageUrl = Race1SB;
    ImageRace2.ImageUrl = Race2SB;
    Map.Text = MapSB;
    GameFrame.Text = GameFrameSB;
    LikeAmount.Text = LikeAmountSB;
    CasterLOGOIMG.ImageUrl = CasterLOGO;
    PlayButton.AlternateText = "yay";

}
protected void PlayButton_Click1(object sender, ImageClickEventArgs e)
{

    Image img = (Image)sender;
    string tt = img.AlternateText;
    int j = 0;
    Response.Redirect("default2.aspx");
   // lb.Text = img.ClientID;

}

}

aspx页面:

<asp:DataList ID="WatchLaterDL" runat="server" DataSourceID="SDSWatchLater" RepeatColumns="1">
                <HeaderTemplate>
                </HeaderTemplate>
                <ItemTemplate>
                    <Search:Bullet runat="server" EnableViewState="false" ID="SearchResults" Player1NameSB='<%#Bind("Player_Name") %>'
                        Player2NameSB='<%#Bind("Expr1") %>' CasterNameSB='<%#Bind("Caster_Name") %>'
                        Race1SB='<%#Bind("Race_1") %>' Race2SB='<%#Bind("Race_2") %>' MapSB='<%# Bind("Map")%>'
                        GameFrameSB='<%#Bind("Game_Frame") %>' LikeAmountSB='<%#Bind("Like_Amount") %>'
                        CastURLSB='<%#Bind("Cast_URL") %>' CasterLOGO='<%#Bind ("Caster_LOGO") %>' />
                </ItemTemplate>
                <SeparatorTemplate>
                    <br />
                </SeparatorTemplate>
            </asp:DataList></div>

问题是,在执行此操作后,当我单击按钮时没有任何反应,我的意思是,在调试时,我从未进入OnButton_Click事件。如果我删除 - &gt; this.ID =“Result”+ i.toString();和 - &gt; PlayButton.ID = i.ToString();没关系。

如果您有可能需要评估我的问题,请高兴地证明并需要额外的代码。  非常感谢你的帮助。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

我的解决方案是这样的:

而不是使用图像按钮,我使用了超链接。

usercontrol的on_Load事件,我分配给超链接的src属性我的用户控件的所有属性并使用

传递

查询到另一页。