gridview,gridview获取选中行的值

http://www.itjxue.com  2023-01-24 10:49  来源:未知  点击次数: 

在GridView中编辑控件怎么使用。

这个简单 选择gridview控件 然后属性 看到那个闪电符号点一下 双击RowEditing这个 然后进入后台 写如下代码 GridView1.EditIndex = e.NewEditIndex;//使当前行进入编辑状态 BindData();//重新绑定数据 这个不能忘 然后 进入编辑状态后还有更新和取消事件粘贴一段给你看看 /// summary

/// 取消编辑事件

/// /summary

/// param name="sender"/param

/// param name="e"/param

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{

GridView1.EditIndex = -1;

isbool = false;

BindData();

}

/// summary

/// 编辑事件

/// /summary

/// param name="sender"/param

/// param name="e"/param

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex; BindData();

}

/// summary

/// 更新事件

/// /summary

/// param name="sender"/param

/// param name="e"/param

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{//更新事件中获取值的方法如下 string linkname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("TextBox1")).Text.Replace("'", "").Trim();//获取第一列中TextBox1控件的值string linkurl = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Replace("'", "").Trim();//获取第二列的值 这个是编辑状态下 自动生成的控件获取方法上面那个是编辑模板里面的控件 }

gridview控件的功能是什么

本文将由达内的讲师详细为您介绍关于的相关常识。 我们在很多的第三方控件中都会发现有点击列头进行排序的功能,其实,在GridView控件中也是可以实现的,下面我们就看看如何在GridView控件中实现排序的示例。 以下为实现在GridView控件排序功能的全部代码,各位只需复制粘贴到vs中即可运行。 前台代码: asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" OnSorting="GridView1_Sorting" Columns asp:BoundField DataField="身份证号码" HeaderText="用户ID" SortExpression="身份证号码" / asp:BoundField DataField="姓名" HeaderText="用户姓名" SortExpression="姓名"/ asp:BoundField DataField="员工性别" HeaderText="性别" SortExpression="员工性别"/ asp:BoundField DataField="家庭住址" HeaderText="家庭住址" SortExpression="家庭住址"/ /Columns

如何在GridView中实现多选

GridView实现跨页多选,参考如下:

JS 前台:

//GridView中实现多选效果

function?CheckAllC(oCheckbox)?{

????var?GridView1?=?document.getElementById('gvDataList');

????for?(i?=?1;?i??gvDataList.rows.length;?i++)?{

????????GridView1.rows[i].cells[0].getElementsByTagName("INPUT")[0].checked?=?oCheckbox.checked;

????}

}

后台:

1:GridView翻页PageIndexChanging事件中调用获取多选主键信息,还需要判断一下,翻页之前是否已经有数据选中。具体方法如下:

方法如下:

#region 存储GridView翻页数据主键

??????? /// summary

??????? /// 存储GridView翻页数据主键

??????? /// /summary

??????? private void RememberOldValues()

??????? {

??????????? Dictionarystring, string list = Session["SelectedObject"] as Dictionarystring, string;

??????????? if (list == null)

??????????? {

??????????????? list = new Dictionarystring, string();

??????????? }

??????????? foreach (GridViewRow row in gvDataList.Rows)

??????????? {

??????????????? CheckBox cb = (CheckBox)row.FindControl("chkSelect");

??????????????? string user = gvDataList.DataKeys[row.RowIndex].Value.ToString();

????????????????if (Session["SelectedObject"] != null)

??????????????????? list = (Dictionarystring, string)Session["SelectedObject"]; //很重要

??????????????? if (cb.Checked)

??????????????? {

??????????????????? if (!list.ContainsKey(user))

??????????????????? {

??????????????????????? list.Add(user, "null");

??????????????????? }

??????????????? }

??????????????? else

??????????????? {

??????????????????? if (list.ContainsKey(user))

??????????????????? {

??????????????????????? list.Remove(user);

??????????????????? }

??????????????? }

??????????????? if (list != null list.Count 0)

??????????????????? Session["SelectedObject"] = list;

??????????? }

??????? }

??????? #endregion

附带GridView自带分页:

#region? 分页

??????? protected void gvDataList_PageIndexChanging(object sender, GridViewPageEventArgs e)

??????? {

??????????? RememberOldValues();

??????????? // 得到该控件

??????????? GridView theGrid = sender as GridView;

??????????? int newPageIndex = 0;

??????????? if (e.NewPageIndex == -3)

??????????? {

??????????????? //点击了Go按钮

??????????????? TextBox txtNewPageIndex = null;

//GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow

??????????????? GridViewRow pagerRow = theGrid.BottomPagerRow;

if (pagerRow != null)

??????????????? {

??????????????????? //得到text控件

??????????????????? txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;

??????????????? }

??????????????? if (txtNewPageIndex != null)

??????????????? {

??????????????????? //得到索引

??????????????????? newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;

??????????????? }

??????????? }

??????????? else

??????????? {

??????????????? //点击了其他的按钮

??????????????? newPageIndex = e.NewPageIndex;

??????????? }

??????????? //防止新索引溢出

??????????? newPageIndex = newPageIndex 0 ? 0 : newPageIndex;

??????????? newPageIndex = newPageIndex = theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

//得到新的值

??????????? theGrid.PageIndex = newPageIndex;

//重新绑定

??????????? BindItems(0, NodeID, strSearch);

??????? }

??????? #endregion

2:点击提交按钮时,需要获取下GridView中的多选主键ID

#region 获取单选按钮选中的主键ID

??????? /// summary

??????? /// 获取单选按钮选中的主键ID

??????? /// /summary

??????? /// returns/returns

??????? private void GetPkID()

??????? {

??????????? Dictionarystring, string list = Session["SelectedObject"] as Dictionarystring, string;

??????????? if (list == null)

??????????? {

??????????????? list = new Dictionarystring, string();

??????????? }

??????????? foreach (GridViewRow row in gvDataList.Rows)

??????????? {

??????????????? CheckBox cb = (CheckBox)row.FindControl("chkSelect");

??????????????? string user = gvDataList.DataKeys[row.RowIndex].Value.ToString();

??????????????? if (cb.Checked)

??????????????? {

??????????????????? if (!list.ContainsKey(user))

??????????????????? {

??????????????????????? list.Add(user, "null");

??????????????????? }

??????????????? }

??????????????? else

??????????????? {

??????????????????? if (list.ContainsKey(user))

??????????????????? {

??????????????????????? list.Remove(user);

??????????????????? }

??????????????? }

??????????? }

??????????? ViewState["SelectedObject"] = list;????

??????? }

??????? #endregion

(责任编辑:IT教学网)

更多

推荐Illustrator教程文章