Respond to Button Events in DataList
After some help and reviewing of the code I got this working without any errors. However when I push the button nothing happens, the text-box ain't updated.
I also wounder how i can access the data inside the DataList so that I can manipulate it inside the "DataList1_ItemCommand" function.
<p>
<asp:TextBox ID="NameTextBox" runat="server" CssClass="textEntry"
TextMode="SingleLine" Rows="0" Height="20px" Width="250px" Enabled="False"></asp:TextBox>
<asp:DataList
ID="DataList1"
runat="server"
RepeatColumns="1" CellPadding="4" ForeColor="#333333"
GridLines="Both" Height="132px" Width="427px">
<HeaderTemplate>
Data
</HeaderTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "ref") %>
<%# DataBinder.Eval(Container.DataItem, "name") %>
<%# DataBinder.Eval(Container.DataItem, "city") %>
<%# DataBinder.Eval(Container.DataItem, "ip") %>
<%# DataBinder.Eval(Container.DataItem, "timestamp") %>
<asp:Button ID="manage" runat="server" CommandName="manageWiki" Text="Granska" Visible="True" />
</ItemTemplate>
<AlternatingItemStyle BackColor="White" ForeColor="#284775" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle Font-Bold="true" Font-Names="Arial" BackColor="#5D7B9D"
ForeColor="White" />
<ItemStyle Font-Names="Arial" Font-Size="Small" BackColor="#F7F6F3"
ForeColor="#333333" />
<SelectedItemStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:DataList>
</p>
And code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//getSuggestions fill the DataList with data
getSuggestions("SELECT [ref], [city], [name], [timestamp], [ip] FROM [table1] ORDER BY timestamp");
}
}
protected void DataList开发者_开发技巧1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "manageWiki")
{
//Just update the TextBox
NameTextBox.Text = "ref that is inside the Datalist1";
}
}
Your PageLoad
is a little wacky I think. You are doing DataList1.DataBind();
on the initial load with nothing assigned to DataList1
to bind.
Also getSuggestions
does a bind everytime as well. You don't need to bind everytime assuming you don't have ViewState
disabled in the DataList
control (which it looks like you don't) or a parent control.
Usually this error occurs if you modify the data that has been served up via client side scripting and are posting it back and it is not matching up with what the ViewState
is expecting... are you sure this is all the code?
EDIT: I suggest commenting out as much code as you can to just perform the function you are having issues with. You are posting a lot of code that is probably just clouding the issue. Get it down to a very basic subset of code and then post the code and steps to recreate because as you have it I don't see any issues. Are you doing any javascript code that is modifying any of the data that would be posted back?
精彩评论