Link to home
Start Free TrialLog in
Avatar of Abdu_Allah
Abdu_Allah

asked on

How to refresh updatepanel content when user click on table row within that control?

Hi, I have updatepanel control which contains a table, I want to execute a serverside function and then refresh that update control once user double click on a table row, how can I do that?
Avatar of AmarIs26
AmarIs26

If your Table is inside the update panel and your Table is initiating the postback to the server then the updatepanel will automatically update providing the UpdateMode is not set or is not set to Confitional.

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Table... />
</ContentTemplate>
</asp:UpdatePanel>
If it is set to Conditional then in your server side method you must update the updatepanel by calling the method.
UpdatePanel1.Update();
If your table is outside the updatepanel and you still want to update, then you must set the UpdateMode="Conditional" and then register the Table control for Asyncpostback using the scriptmanager. (Note: that if your updatepanel's updatemode is not set or is set to always then you dont need to register for asyncpostback).

ScriptManager.RegisterAsyncPostBackControl(Table...
 
Avatar of Abdu_Allah

ASKER

My table is inside the updatepanel. I want to refresh the content of updatepanel once user double click on a row in that table.
Sorry the above solution assumed you already are calling the server side function and your updatepanel is not updating. If you just want to know how to call the server side function from row double click then...

Also you can call the following code in javascript from the doubleclick event of the html row and then add this javascript to your page.
<script language="javascript" type="text/javascript">
function CallServerFunction(rowID,arg)
{
  __doPostBack(postBackDataID,arg)
}
</script>
So imagine your table is like this. Because it is a htmltable we need to add runat="server" and the id property

<table style="width: 100%;" runat="server" id="testTable">
<tr>
<td>
asdsadasd
</td>
</tr>
</table>
in the serverside code behind. This is basically trying to see if a row has been double clicked, if so call our ServerFunction.

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

string eventTarget = Request.Form["__EVENTTARGET"];
if (!string.IsNullOrEmpty(eventTarget) && eventTarget== "ServerFunction")
{
//Call Server Function
}
}
 Finally, Ensure that we attach double click event handlers to each table row. This will call the javascript function passing the serverside function name to execute.

protected override void OnPreRender(EventArgs e)
{
foreach (HtmlTableRow row in testTable.Rows)
{
row.Attributes.Add("ondblclick" , "javascript:CallServerFunction('ServerFunction','row1');");
}
base.OnPreRender(e);
}
 
 
 
 
Thank you for that code but I noticed that the postback refresh the entire page not only Updatepanel content, how to make it just refresh just updatepanel content only. Something else how can I get the arg that I sent from via CallServerFunction function.
Sorry the previous javascript had an error. here is the code that works fully with args being passed. Basically you need to add a hiddenfield in the update panel which will hold your arg and server name

<asp:HiddenField ID="HiddenField1" runat="server" OnValueChanged="HiddenField1_ValueChanged" />
Then when you call the CallServerFunction (javascript one) it will populate the hidden field with the server function id (not recommended use some sort of key name) + arg. This can be retrieved on teh server side in the ValueChanged event of the hidden field.

<script language="javascript" type="text/javascript">
function CallServerFunction(rowID,arg)
{
var hiddenID = '<%= HiddenField1.ClientID %>';
var hiddenField = document.getElementById(hiddenID);
hiddenField.value = rowID + ","+arg;
__doPostBack(hiddenID,arg)
}
</script>
 So all you  need to do is
HiddenField1.Value
and split that with "," to separate the function name and arg.
 
 
ooops I forgot the runat server property for the updatepanel, it works fine but how can I get the arguments I sent via __doPostBack(postBackDataID,arg)  in the Onload function that ?
Also you dont need the code in the OnLoad event. All that can go. :)
Actually I want to retreive the arguments data in the Onload function how can I do that?
then you could use the piece of code i asked you to remve
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

string eventTarget = Request.Form["__EVENTTARGET"];
if (!string.IsNullOrEmpty(eventTarget) && eventTarget== "HiddenField1")
{
      string value = HiddenField1.Value;
//     value.Split(new string[]{","});
}
}  
AmarIs26, if I place an asp:button inside the updatepanel and then click it the postback event fired successfully and only the updatepanel area is refreshed but if I double click on the table row the postback event fired successfully but the entire page is refreshed!
ASKER CERTIFIED SOLUTION
Avatar of AmarIs26
AmarIs26

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you man.