views:

58

answers:

1

Hi,

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 DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "manageWiki")
    { 
        //Just update the TextBox
        NameTextBox.Text = "ref that is inside the Datalist1";
    }
}
A: 

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?

Kelsey
Okay i figured out what you meant with "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" and it works. I'll edit the code above.
Mikael
@Mikael so is your problem solved? If this answer helped you don't forget to set it as the accepted answer or even give it an up vote :)
Kelsey
@Kelsey it was partly solved but i realized that the question I asked was wrong so I updated it, maybe I should have started a new question?
Mikael
@Mikael well the button isn't firing because you don't have the `ItemCommand` event wired up in your `DataList`. There should be a property like `OnItemCommand="DataList1_ItemCommand"`. As for accessing the data in the `DataList`, you shouldn't be accessing it directly. If you want some data from it, you should be front loading it so that it is passed into you `ItemCommand` event and don't need to go looking for it.
Kelsey
Works like a charm, thanks!!
Mikael