i need help in 2 things:
first i have several imagebutton so dynamically i change their ID but when i click in a button the ID reset to default = the ID before run time,so because the page reset because the page returned to the server ,but is their any method to remain this ID? because in the form load i write: if not page.postback <code> end if
so in the form load i change the iD,so any idea how to remain them?
and i want the server side i don't prefer javascript because i know what i need in asp.net not javascript and i have asp.net controls not input type so i want asp.net controls.
second:
i need to know the ID of the imagebutton who caused the postback but with no result i have an empty string.
so really i need your advices.
thank you.

Dani AI

Generated

Short answer: do not change a server control's ID only inside the initial Page_Load. ASP.NET rebuilds the control tree from the markup on every request and the control ID is not persisted in ViewState, so any runtime change must be applied early and on every request so postback wiring and viewstate restoration work.

What to do

  • Move the ID assignment into initialization (OnInit / Page_Init) and perform it on every request (not wrapped in Not IsPostBack). That makes the control tree stable before ASP.NET restores postback data and events.
  • Prefer wiring server Click handlers (or a single handler shared by several buttons) instead of relying on client-side names. That is the simplest, most robust server-side approach for knowing which button was pressed.

If you need to detect the postback source generically (image buttons submit coordinates), check Request.Form for __EVENTTARGET first, then look for keys that end with ".x" or ".y" (image input sends those). Remove the suffix to get the posted name and then locate the server control (mind naming containers — you may need a recursive FindControl or to compare UniqueID).

Example patterns (conceptual)

  • Set IDs in OnInit every request so UniqueID/ClientID are stable.
  • To find an ImageButton that caused postback, inspect Request.Form keys for ".x"/".y", strip the suffix and resolve the control.

Cautions

  • Changing a server ID between requests breaks viewstate/event hookup. Keep the ID stable across all requests.
  • If controls are nested, Request.Form keys use UniqueID; finding the control may require walking NamingContainers.

See the ASP.NET page lifecycle for why initialization timing matters: ASP.NET Page Life Cycle Overview

Notes on this thread

  • : the reset you saw is expected given the lifecycle.
  • : asking for the code is right — the fix is to initialize IDs in Init and/or use server Click handlers.

Recommended Answers

All 2 Replies

Can you show the code then i might able to usderstand the problem better and mention in it what you want to do

i want this:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <asp:ImageButton ID="ImageButton1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
     
    </form>
</body>
</html>

the code behind :

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            ImageButton1.ID = "name"
        End If
    End Sub
End Class

so run the page and view the source you see that the id of the imagebutton is "name" so click the button again the id of the imagebutton reset to "imagebutton1" and i wont this.
so it's a part of a big project so i want the ID to remain even the page is postback to the server.
thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.