DataList更新按钮不起作用。 [英] DataList Update Button is Not Working.

查看:68
本文介绍了DataList更新按钮不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我正在研究数据列表。我想编辑一些数据并在更改后更新。编辑按钮工作正常,但更新按钮无法调用,我尝试了很多网站,但我没有正确的解决方案。这是我目前的部分: -

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: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




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)
    {

    }









我的问题是编译器没有去更新命令和按钮编码







i等待..





紧急删除 - OriginalGriff [/ edit]





my problem is compiler not go to updateCommand and Button coding



i am waiting ..


[edit]Urgency deleted - OriginalGriff[/edit]

推荐答案

从您的标记中,需要删除来自datalist的EnableViewState =False。因为需要datalist update事件,所以还要确保为您的页面/站点启用了viewstate。默认情况下,如果您明确禁用它,则会启用它。

接下来你需要在页面加载事件中检查IsPostBack方法。

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.
if (!Page.IsPostBack)
           {
               DataList1.DataSource = mydata();
               DataList1.DataBind();
           }







如果你这样做,那么你的更新事件将被触发并运行代码与此事件绑定的事件处理程序。我检查并调试你的代码,发现它正在解雇,如果这是你唯一的问题,那么它将在你的代码更改后修复。

但我发现DataList1_EditCommand和DataList1_UpdateCommand相同的代码并没有找到任何逻辑。我猜你和你分享的不是真正的代码。




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.


Ahasan Habib指出了解决问题的方法。但是,如果你故意不想因为某种原因使用ViewState,那么应该使用hiddenfield进行解决方法。



类似下面的步骤。

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();





注意:这只是First解决方案的另一种选择,以防ViewState需要假。



希望这有帮助...



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

Hope this helps...


你的代码没有错。

全部你需要把 If(!PageIspostback)然后在大括号之间放置你的datalist databind和datasource事件。

。因为在任何命令事件页面加载事件之前都会运行你是绑定源列表,它只会从中返回。



希望这有助于!!!
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 !!!


这篇关于DataList更新按钮不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆