0

Whenever I click one of the delete linkbuttons, the e.CommandArgument always retains the value of "". I was reading somewhere that you can't use <%# in Command Arguments but I have seen several examples were this has worked. Any suggestions?

In ASPX

 <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
        <ItemTemplate>
            <asp:Label ID="dataLabel" runat="server" Text='<%# Eval("data") %>' />
            <br />
            <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
            &nbsp;|&nbsp;
            <asp:Label ID="Column1Label" runat="server" Text='<%# Eval("Column1") %>' />
            <br />
            <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
            <LoggedInTemplate>
                <asp:LinkButton CLASS="DeleteButton" runat="server" OnCommand="Delete" CommandArgument='<%# Eval("id") %>' ViewStateMode="Disabled">Delete</asp:LinkButton>
            </LoggedInTemplate>
            </asp:LoginView>
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 

            SelectCommand="SELECT [id],[name], [data], convert(varchar, [date], 101) FROM [Announcements] ORDER BY [date] DESC">
    </asp:SqlDataSource>

IN ASPX.CS

using (SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString))
        {
            SqlCommand command = conn.CreateCommand();
            command.CommandText = "DELETE FROM Annoucnements WHERE id=@id";
            command.CommandType = System.Data.CommandType.Text;

            command.Parameters.Add(new SqlParameter("id", e.CommandArgument));

            conn.Open();
            command.ExecuteNonQuery();
        }

As suggested by Royi Namir, changing EnableViewState to true fixed the problem.

11
  • 2
    What do you see in the HTML source of the button? Commented May 28, 2013 at 14:22
  • Just as an aside, you're already using an SqlCommand but not using parameters, you really should use parameters for Sql queries, especially where the data comes from the client. Look at Step 3 in this article: msdn.microsoft.com/en-us/library/ff648339.aspx it's old but it still applies =] (Yes I know it's unlikely that someone is going to change the code in your page but you never know, especially if it's a public-facing website.) Commented May 28, 2013 at 14:22
  • <a CLASS="DeleteButton" href="javascript:__doPostBack(&#39;ctl00$MainContent$DataList1$ctl00$HeadLoginView$ctl01&#39;,&#39;&#39;)">Delete</a> Commented May 28, 2013 at 14:24
  • Here is a similar post on the asp.net forums. It might provide a solution. forums.asp.net/t/1303845.aspx/1 Commented May 28, 2013 at 14:33
  • 1
    change to EnableViewState="true" Commented May 28, 2013 at 15:03

2 Answers 2

1

After looking around for a little bit, I have found a few posts that say you can't use the <%# ... %> syntax inside server tags, which is clearly wrong as you are using it for the Text property and I'm sure I've done this exact same thing before. Anyway, you can use the ItemDataBound event on your DataList:

 <asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="DataList1_ItemDataBound">
        <ItemTemplate>
            <asp:Label ID="dataLabel" runat="server" Text='<%# Eval("data") %>' />
            <br />
            <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
            &nbsp;|&nbsp;
            <asp:Label ID="Column1Label" runat="server" Text='<%# Eval("Column1") %>' />
            <br />
            <asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
            <LoggedInTemplate>
                <asp:LinkButton CssClass="DeleteButton" runat="server" ID="lbDelete" OnCommand="Delete" CommandArgument='<%# Eval("id") %>' ViewStateMode="Disabled">Delete</asp:LinkButton>
            </LoggedInTemplate>
            </asp:LoginView>
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 

            SelectCommand="SELECT [id],[name], [data], convert(varchar, [date], 101) FROM [Announcements] ORDER BY [date] DESC">
    </asp:SqlDataSource>

Notice I've added ID="lbDelete" to your LinkButton (and also changed CLASS to CssClass for you) which is needed to find the control in our code behind:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) // this is to ensure that we're looking in an item that will contain controls, and not the header or footer
    {
        LoginView headLoginView = (LoginView)e.Item.FindControl("HeadLoginView") // FindControl is not recursive so we need a reference to a control we can look in to find the button
        LinkButton lbDelete = (LinkButton)headLoginView.FindControl("lbDelete");

        if (lbDelete != null) // check for a null otherwise this code will fail if the user is not logged in, because the controls inside the LoggedInTemplate will not be rendered
        {
            lbDelete.CommandArgument = DataBinder.Eval(e.Item.DataItem, "id").ToString(); // set the CommandArgument on the button using DataBinder.Eval
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

it doesn't like the e.Row(Does not contain a definition for Row)
Sorry, got mixed up between Repeaters and DataLists, just change Item to Row, I've edited my answer to reflect.
Also, did you try Royi's suggestion about re-enabling the ViewState for that control? I'm interested to find out whether the CommandArgument is stored in ViewState....
Also it still seems to have to same problem. I added the code then removed my manual command argument. Was there anything else that was necessary?
No, that should have done it... Could you remove any bits you've added to do with ViewState from your code and try both methods again (this and the <%# Eval("id") %> method), that could be the issue, as weird and broken as that is =]
|
0

You simply need the DataKeyField to uniquely identify your row in order to perform Delete operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.