Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the usercontrol inside updatepanel problem

Tags:

c#

asp.net

i have a usercontrol inside an updatepanel. when i click some button this usercontrol will be loaded. and the usercontrol itself has anither button in it. the problem is that the usercontrol code behind is never executed and when the button is clicked the usercontrol disappears.

i know this is a common problem. but i have not found a good detailed solution.

i appreciate it.

like image 344
HTB Avatar asked Apr 01 '11 00:04

HTB


2 Answers

In your user control, just use a standard HTML button like this:

<input type="button" id="myButton" onclick="clickTheButton();" value="Click Me"/>

this will invoke the javascript method "clickTheButton" which can look like this:

<script type="text/javascript">
function clickTheButton() {

    var Sender = window.event.srcElement;

    //here you can go gather any other values that you need to support the post back

    var PostBackData = textboxCity.value + "|" + selectState.value
    if(confirm("are you sure?"))
    {
            __doPostBack(Sender.ID, PostBackData)
    }
}
</script>

So now you are going to invoke a postback from Javascript, identifying the Sender Control and command arguments. These values are passed with the post back as __EventTarget and __EventArgument and are available from the httpRequest in the page load like:

protected void Page_Load(object sender, EventArgs e)
{
  string controlName = Request.Params.Get("__EVENTTARGET");
  string[] commandArguments = Request.Params.Get("__EVENTARGUMENT").split('|')
}

Once you are in page load with whatever values you accumulated into commandArguments you should be capable of invoking whatever additional methods you desire.

This should get you pointed in the right direction.

Cheers,

CEC

like image 160
Cos Callis Avatar answered Oct 12 '22 23:10

Cos Callis


Loading user-control dynamically in update-panel is tricky .

The Best solution is to save it in ViewState , Here is a tutorial and sample application .

like image 21
Mostafa Avatar answered Oct 13 '22 01:10

Mostafa