Reputation: 1
I have tested buttons inside the repeater in the update panel. If you add asyncpostback <Trigger></Trigger>
for buttons you're getting an error. Button couldn't be found.
Well, I am using two linkbutton inside repeater. i added javascript code on OnClick="__doPostback(linkbutton1, '');"
it's working without fullpostback. Button is clicking and i am showing message.
It's working without problem. Not facing with FullPostBack problem.
<asp:LinkButton runat="server" ID="linkbutton" OnClientClick="__doPostback(linkbutton1.UniqueID, '');" />
I just want to use function not directly code inside OnClick and problem has started.
Example:
function postback(){
__doPostback(linkbutton1.UniqueID, '');
}
ASP.NET Code:
<asp:UpdatePanel ID="update_buttons" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Repeater ID="repeaterb" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="linkbutton" OnClientClick="return postback();" />
<asp:LinkButton runat="server" ID="linkbutton1" OnClick="linkbutton1_Click"/>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
Page.ClientScript.RegisterStartupScript(this.GetType(), "postback", "postback()", true);
Upvotes: 0
Views: 1113
Reputation: 49309
You using return postback() in onclient click, but that routine has to return true or false. If true, then server side button code and "partial" post-back occurs.
Remember, a plane jane standard asp.net button in a update panel DOES cause what we call a partial post-back (even the page load event fires). So, I don't see any particular difference between using a server side asp.net button, and that of using a OnClientClick which does a post-back also!!! - they both should do the same thing!
Note sure what the line of code about running a client script by injecting a client side script (register script - not sure what that has to do with this question? (somewhat confusing). You can have one button run both client side code and server side code combined into one button as I show below.
So, for the OnClientClick button, you would/could do this:
<asp:LinkButton runat="server" ID="linkbutton" OnClientClick="postback();return false;" />
So, if the OnClientClick returns true - then the update panel postback WILL ocurr.
And if OnClientClick returns false - the the update panal postback will NOT occur.
So, lets drop in a test button, and run some server side code:
So, we now have this:
<asp:UpdatePanel ID="update_buttons" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Repeater ID="repeaterb" runat="server">
<ItemTemplate>
<asp:TextBox ID="HotelName" runat="server" Text = '<%# Eval("HotelName") %>' ></asp:TextBox>
<br />
<asp:LinkButton runat="server" ID="linkbutton" OnClientClick="return postback();" Text="Link button"/>
<br />
<asp:LinkButton runat="server" ID="linkbutton1" OnClick="linkbutton1_Click" Text="Link btn 1" />
<br />
<asp:Button ID="Button1" runat="server" Text="Server Test button"
onclick="Button1_Click"/>
<div style="clear:both;height:15px"></div>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
So the button click servre side code could be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadMyRepeater();
}
void LoadMyRepeater()
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from tblHotels WHERE ID = 23",
new SqlConnection(Properties.Settings.Default.TEST3)))
{
cmdSQL.Connection.Open();
repeaterb.DataSource = cmdSQL.ExecuteReader();
repeaterb.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Button MyButton = (Button)sender;
RepeaterItem rRow = (RepeaterItem)MyButton.Parent;
Debug.WriteLine("Row click Index = " + rRow.ItemIndex.ToString());
Debug.WriteLine("Hotel Name = " + ((TextBox)rRow.FindControl("HotelName")).Text);
}
So we see this:
And clicking on that button, we get this:
Output window:
Row click Index = 0
Hotel Name = Ramada Lodge
Now, we can add a on-click click code stub to that simple button. and we can thus have both client side code + server side code run for that one button click.
And we can even conditional have that button code run by having the OnClientClick code run.
The button could become this:
<asp:Button ID="Button1" runat="server" Text="Server Test button"
onclick="Button1_Click"
OnClientClick="return myprompt();"/>
and
<script>
function myprompt() {
return confirm("Do you really want to run server code?");
}
</script>
And then when you click on that button you get this:
So, now if you click ok, server side code for button runs, but if you hit cancel, then the server side code button does not run.
But, be careful with prompts. If you use a jQuery dialog, toast message routine?
Those routines do NOT wait, and you have to change that code a bit, since it runs async, and most jquery dialogs and message boxes do NOT wait, and hence you can't conditional prompt the user that way (I can post how you can do this if you wish).
So, it not clear if you just want to drop in, + use a standard asp.net button, but ONLY call client side code?
As noted, then make sure the expression returns false. or as noted, use:
OnClientClick="postback();return false;"
I
Upvotes: 0