在listview itemcommand处获取datakey值 [英] getting datakey value at listview itemcommand

查看:114
本文介绍了在listview itemcommand处获取datakey值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从数据库中填充了listview.我正在尝试获取列表框bt的datakey值,而我正在获取null值:代码为:

i have populated listview from database.i am trying to get the datakey value of listbox bt i am getting null value:the code is:

protected void Maillist_ItemCommand(object sender, ListViewCommandEventArgs e)
   {
       if (e.CommandName == "Delete")
       {
           populatelistview();
           Label lblid=(Label)e.Item.FindControl("lblid");
           string id = Maillist.SelectedValue.ToString();
           SqlConnection conn = new SqlConnection();
           conn.ConnectionString = strconn;
           string str = "delete from Companymails_Master where Mailid=''" + Maillist.DataKeys.ToString() + "''";
           SqlCommand cmd = new SqlCommand(str,conn);
           conn.Open();
           cmd.ExecuteNonQuery();
           conn.Close();

           populatelistview();
       }
   }

plz指南...如果我错了,我只是想在单击
时删除该行 删除按钮.

plz guide...where m i wrong.i simply want to delete the row on clicking the
delete button.

推荐答案

尝试此

try this

 protected void EmployeesListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "AddToList"))
    {
      // Verify that the employee ID is not already in the list. If not, add the
      // employee to the list.
      ListViewDataItem dataItem = (ListViewDataItem)e.Item;
      string employeeID = 
        EmployeesListView.DataKeys[dataItem.DisplayIndex].Value.ToString();

      if (SelectedEmployeesListBox.Items.FindByValue(employeeID) == null)
      {
        ListItem item = new ListItem(e.CommandArgument.ToString(), employeeID);
        SelectedEmployeesListBox.Items.Add(item);
      }
    }
  }


//Other way is 

<asp:LinkButton ID="LinkButton1" CommandName="Insert" CommandArgument=''<%#Eval("CatId")%>''
                 runat="server">Edit</asp:LinkButton>

//in the item command Event 

   if (e.CommandName == Edit)
       {

 int CatId = int.Parse(e.CommandArgument.ToString());
}


您是否提到过aspx ListBox控件中的DataKey属性或DataKey必须与数据库中的表字段相同.

谢谢与问候,
Balwant
Have you mention DataKey attribute in the aspx ListBox Control or DataKey must be same as that of table field in the database.

Thanks & Regards,
Balwant


尝试使用此查询

Try this for your query

string str = "delete from Companymails_Master where Mailid=''" + Maillist.DataKeys[e.ItemIndex].ToString() + "''";



希望这会有所帮助.......



Hope this helps .......


这篇关于在listview itemcommand处获取datakey值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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