简体   繁体   中英

Why OnClick event of a button inside usercontrol not happening

.NET pros out there

I have a problem that i would like to share with you and if possible get an advice:

I am loading a usercontrol dynamically using datalist. i am loading this usercontrol for each row i get from the database, it could be 30 different instanses of this control.

when a user click the button, i want to be able to get the values of this specific usercontrol's properties, in my understanding, i heed the usercontrol ID to do that, thats why i want to assign my "custom id" to each loaded control so i will be able to find it later. maybe i am missing something, maybe this is not the best way, im open to suggestions. anyway.....

UserControl code behind:

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;

}

}

The aspx page:

<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>

the problem is, after doing this, when i click the button nothing happens, i mean, when debugging, i never get inside the OnButton_Click event. if i delete the --> this.ID = "Result" + i.toString(); and the--> PlayButton.ID = i.ToString(); it's ok.

if there anything elese you might need to evaluate my problem, ill gladly prove and additional code needed. thank you very much for your help.

my solution was like this:

instead of using an image buttom, i used Hyperlink.

on_Load event of the usercontrol, i assigned to the src Attribute of the hyperlink all the properties of my user control and passed those using

querystring to another page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM