开发者

repeater control causing Invalid postback or callback argument

开发者 https://www.devze.com 2023-02-07 22:45 出处:网络
I have read through many of the questions related to my situation but I have not found one that does. I currently have a repeater with 4 bound items to it, clicking on a button in the repeater causes

I have read through many of the questions related to my situation but I have not found one that does. I currently have a repeater with 4 bound items to it, clicking on a button in the repeater causes my error, below is the markup and codebehind. Can someone explain to me why this is happening and the fix for it? (I have excluded the page decleration and just included the page_load event. let me know if i should cut and past full code in)

<!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">
<开发者_如何学Go;head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:Repeater ID="rptFoo" runat="server">
      <ItemTemplate>
        <asp:Button ID="btnfoo" runat="server" />
      </ItemTemplate>
    </asp:Repeater>
  </div>
  </form>
</body>
</html>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim items As New List(Of String)
items.Add("test1")
items.Add("test2")
items.Add("test3")
items.Add("test4")
rptFoo.DataSource = items
rptFoo.DataBind()
End Sub


"Invalid postback or callback argument" with Databound controls

answers my question incase anyone else needs this


That happens because the Repeater is getting datasource (binding) as you perform the item command operation. Just add if(not ispostback) to the page_load method.


I will repeat the answer here so you don't need to link to

The problem is loading the data for the control in the page Load event and calling the DataBind() method. However it appears that if the DataBind() method is called before the events are raised the above exception is generated as the control naming has changed.

The solution is to change this to if(!IsPostback) DataBind() and then call the DataBind() method at the end of the event handler. You would need to call it most of the time anyway at the end of the handler to affect the changes.

If this is not your problem, and you are modifying controls client side using JavaScript, check out this article.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号