Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
Hello every body. I working on Data List. I want to edit the some data and update then after some changing. Edit button is working fine but update button can't call, i have try many site but i am failed correct solution. here is my present part : -
ASP.NET
  <asp:DataList ID="DataList1" runat="server"
         
            onitemcommand="DataList1_ItemCommand" EnableViewState="False" 
            ondeletecommand="DataList1_DeleteCommand" oneditcommand="DataList1_EditCommand" 
            onupdatecommand="DataList1_UpdateCommand">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("id") %>' ></asp:TextBox>
                <br />
                <br />
                <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("role") %>' ></asp:TextBox>
                <br />
                <br />
                <br />
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="update" 
                    EnableViewState="False" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
                
                <asp:Button ID="Button1" runat="server" Text="Button" CommandName="Update" />
                
            </EditItemTemplate>
        <ItemTemplate>
       
    Subject:   <asp:Label runat="server" Text='<%#Eval("id") %>' ID="_id"></asp:Label>


     Role:--  <asp:Label runat="server" Text='<%#Eval("role") %>' ID="name"></asp:Label>
        
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <br />
            <br />
            <asp:Image ID="Image1" runat="server" />
            <br />
            <br />
            <br />
            <asp:Button ID="Btn_edit" runat="server" onclick="Btn_edit_Click" Text="Edit" CommandName="edit" />
        
        
        </ItemTemplate>
        </asp:DataList>


and here is code behind coding

C#
public partial class DataList : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=jogi;Integrated Security=True");
    SqlCommand com;
    SqlDataReader rd;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        DataList1.DataSource = mydata();
        DataList1.DataBind();

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

        //if (edit1)
        ////{
        //// TextBox tt=   DataList1.FindControl("TextBox1") as TextBox;
        //// Response.Cookies[0].Value = tt.Text;
        //}
    }


    public DataTable mydata()
    {
        com = new SqlCommand("testing", con);
        com.Parameters.AddWithValue("@condition", 1);
        com.CommandType = CommandType.StoredProcedure;
        con.Open();
               rd = com.ExecuteReader();
        dt.Load(rd);
        con.Close();
        return dt;

    }
    bool edit1;
    protected void Btn_edit_Click(object sender, EventArgs e)
    {
        edit1 = true;


    }
    protected void save_Click(object sender, EventArgs e)
    {

    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataSource = mydata();
        DataList1.DataBind();

    }
    protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
    {

    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {                                       //here is compiler can't come
        DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataSource = mydata();
        DataList1.DataBind();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {

    }




my problem is compiler not go to updateCommand and Button coding



i am waiting ..


[edit]Urgency deleted - OriginalGriff[/edit]
Posted
Updated 10-Feb-18 19:11pm
v6
Comments
OriginalGriff 20-Dec-13 14:04pm    
Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.
joginder-banger 20-Dec-13 15:47pm    
sir plz help me..

From your markup EnableViewState="False" from datalist need to remove. Because datalist update event it is needed also make sure that viewstate is enabled for your page/site. By default it is enabled if you explicitly disabled it.
Next you need to check IsPostBack method in page load event.
C#
if (!Page.IsPostBack)
           {
               DataList1.DataSource = mydata();
               DataList1.DataBind();
           }



If you do this then your update event will be fired and code run event handler that you bind with this event. I check and debug your code and found it is firing and if this is your only problem then it will fix after your code change.
But i found DataList1_EditCommand and DataList1_UpdateCommand same code and did not find any logic there. I guess that you shared with up are not real code.
 
Share this answer
 
Comments
joginder-banger 21-Dec-13 4:31am    
yes this is a real code.
S. M. Ahasan Habib 21-Dec-13 5:19am    
ok then try with this code and guide which i said above and let me know if it not solve your problem.
Ahasan Habib have pointed out the solution for the issue. But just incase, if you have deliberately wanted not to use ViewState for some reason, then should have a workaround using hiddenfield.

Something like below steps.
- Declare hiddenfield outside datalist and assign the EditItemIndex value without any initial value

- Inside OnEditCommand event 

datalist.edititemlndex = e.item.itemindex;

hiddenfield.Value = Convert.Tostring( e.item.itemindex);

DatabindMethod();

- In Pageload Event:

if (!IsPostBack)
{
    DatabindMethod();
}
else

{
   if (!String.IsNullOrEmpty(hiddenfield.Value)
   {
          datalist1.edititemlndex = Convert.ToInt32(hiddenfield.Value);
          DatabindMethod();
   }
   else
   {
         DatabindMethod();
   }
}

- In OnUpdateCommand event 

UpdateMethod();
datalist.edititemlndex = -1;
hiddenfield.Value = String.Empty;//to exit edit mode
DatabindMethod();


Note : This is just an alternative workout for the First solution just in case ViewState needed to be false.

Hope this helps...
 
Share this answer
 
v2
Nothing wrong in your code.
All you need to put If(!PageIspostback) then in between curly braces put your datalist databind and datasource event.
. Because before any command event page load event will run and here you are binding source to list and in it will get return from only.

Hope This Helps !!!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900