Home
Do you know not to use LinkButton?
  v1.0 Posted at 2/09/2016 4:06 AM by Tiago Araujo
If we want to refresh and data bind the same page from client side, we can use the javascript function calls "__doPostBack". We shouldn't fire this post back in LinkButton. Otherwise, there will be an error.
RightClickLink.gif
Figure: Right click the link with __doPostBack event  ​
PostBack.gif
Figure: New window with incorrect URL

ASPX:
<asp:Panel runat="server" ID="mUpdatePanel" OnLoad="mUpdatePanel_Load">
<asp:Label runat="server" ID="lblTime" />
<br />
<asp:GridView ID="gvList" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
</Columns>
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
</Columns>
</asp:GridView>
<br />
ID:<asp:TextBox ID="txtID" runat="server"/>
Name:<asp:TextBox ID="txtName" runat="server"/>
</asp:Panel>
C#:
protected void mUpdatePanel_Load(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.ToLongTimeString();
ArrayList mList = (ArrayList)ViewState["List"];
if (txtName.Text.Length > 0)
{
Client mClient = new Client();
mClient.ID = Int32.Parse(txtID.Text);
mClient.Name = txtName.Text;
mList.Add(mClient);
ViewState["List"] = mList;
gvList.DataSource = mList;
gvList.DataBind();
}
}

Sample Code​​​

​​​<a href="javascript:__doPostBack('mUpdatePanel','');">Refresh</a>

Bad Code​

<input type="button" onclick="javascript:__doPostBack('mUpdatePanel','');" value="Refresh" />

Good Code​

We have a program called SSW Code Auditor to check for this rule.​


Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.