ASP的GridView,其选择多个行,并检索值 [英] ASP Gridview that selects multiple rows and retrieves the value

查看:215
本文介绍了ASP的GridView,其选择多个行,并检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想尝试允许用户选择我的网格多行,然后将这些选定的值在文本框中显示。我可以得到一个值到文本框,并能够选择一行。我怎样才能改变什么,我已经有了允许多重选择?我想贴什么思维之下添加对象的新实例,让我看到在文本框中的两个值,但它只是重复了第一个选择的值。

 保护无效dropdeadGridView_SelectedIndexChanging(对象发件人,GridViewSelectEventArgs E)
{
    GridViewRow行= dropdeadGridView.Rows [e.NewSelectedIndex]
    GridViewRow ROW1 = dropdeadGridView.Rows [e.NewSelectedIndex]
}
保护无效dropdeadGridView_SelectedIndexChanged1(对象发件人,EventArgs的发送)
{
    GridViewRow排= dropdeadGridView.SelectedRow;
    GridViewRow ROW1 = dropdeadGridView.SelectedRow;
    IDTextBox.Text = row.Cells [1]。文本+,+ row1.Cells [1]。文本;
    loadnumTextBox.Text = row.Cells [2]。文本+,+ row1.Cells [2]。文本;}

ASP code

 < ASP:GridView控件ID =dropdeadGridView=服务器HEIGHT =300像素
        WIDTH =650像素背景色=黑色边框=实
        CELLPADDING =3字体粗体=真前景色=黄色
        onselectedindexchanging =dropdeadGridView_SelectedIndexChanging
        autogenerateselectbutton =真的selectedIndex =0
        onselectedindexchanged =dropdeadGridView_SelectedIndexChanged1>
        < RowStyle Horizo​​ntalAlign =中心/>
        < SelectedRowStyle边框样式=插图BORDERCOLOR =红前景色=红/>
    < / ASP:GridView的>


解决方案

由于您使用ASP.Net Web窗体我建议创建一个placinig还有里面的复选框控件,然后用户点击行,他们将复选框喜欢进行选择。在按钮操点击事件内背后code。通过寻找checkbox.checked属性找到的行。随后读取所需的单元格的数据,并将其追加到文本框。

 < ASP:GridView控件ID =yourGrid=服务器的AutoGenerateColumns =FALSE
                    的DataSourceID =YourSource>
        <柱体和GT;
            < ASP:的TemplateField>
                <&ItemTemplate中GT;
                    < ASP:复选框ID =YourCheckBox=服务器/>
                < / ItemTemplate中>
            < / ASP:的TemplateField>
            < ASP:BoundField的数据字段=名称的HeaderText =名称/>
            < ASP:BoundField的数据字段=ModifiedDate的HeaderText =ModifiedDate/>
            < ASP:CommandField中ShowEditButton =真/>
        < /专栏>
    < / ASP:GridView的>

在这里被更新code后面##

如果你不舒服与jQuery然后用这个去了。

 保护无效CheckBox1_CheckedChanged(对象发件人,EventArgs的发送)
{
复选框CHK =(复选框)发送;
GridViewRow GR =(GridViewRow)chk.Parent.Parent;
txtBox.Text = GridView1.DataKeys [gr.RowIndex] .Value.ToString();
}

修订 - 一个jQuery的解决方案

不过,我认为这样做根据您的要求的最简单方法是使用JQuery。

首先添加一个jQuery脚本到项目中。转到Jquery.com拉下最新的剧本,如果你的项目没有它了。在您的网页标记了头添加裁判。

 <脚本类型=文/ JavaScript的SRC =&GT您的JS文件的源;< / SCRIPT>

然后,在你的身体到底写你JQuery的。

 <脚本类型=文/ JavaScript的>
    $(文件)。就绪(函数(){
    //通过在GridView&GT去捕获click事件; tablerow的> tabledisplay> `最后的复选框 - 抢click事件。
 $('#GrdViewID'TR TD
 输入[ID * =chkSelected]`[类型=复选框]:检查。)在('点击',功能(){
          $('#txtBox)文本($(本).closest('TR')找到('TD')方程(2)的.text());
     });
< / SCRIPT>

I am trying to trying to allow the user to select multiple rows of my grid and then those selected values display in the text box. I can get one value into the textbox and able to select one row. How can I change what I already have to allow multiple selections? I tried whats posted below thinking adding a new instance of the object would allow me to see both values in the textbox but it just repeats the first selected value.

protected void dropdeadGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    GridViewRow row = dropdeadGridView.Rows[e.NewSelectedIndex];
    GridViewRow row1 = dropdeadGridView.Rows[e.NewSelectedIndex];
}
protected void dropdeadGridView_SelectedIndexChanged1(object sender, EventArgs e)
{
    GridViewRow row = dropdeadGridView.SelectedRow;
    GridViewRow row1 = dropdeadGridView.SelectedRow;
    IDTextBox.Text = row.Cells[1].Text + "," + row1.Cells[1].Text;
    loadnumTextBox.Text = row.Cells[2].Text + "," + row1.Cells[2].Text;

}

ASP code

<asp:GridView ID="dropdeadGridView" runat="server" Height="300px" 
        Width="650px" BackColor="Black" BorderStyle="Solid" 
        CellPadding="3" Font-Bold="True" ForeColor = "Yellow" 
        onselectedindexchanging="dropdeadGridView_SelectedIndexChanging"
        autogenerateselectbutton="true" selectedindex="0" 
        onselectedindexchanged="dropdeadGridView_SelectedIndexChanged1" >
        <RowStyle HorizontalAlign="Center" />
        <SelectedRowStyle BorderStyle="Inset" BorderColor="Red" ForeColor="Red" />
    </asp:GridView>

解决方案

Since you are using ASP.Net web forms I would recommend creating an placinig the check box control inside of there then the user clicks the checkbox on the rows they would like to select. In the code behind within the butto click event find the rows by looking for the checkbox.checked property. Afterwards read the desired cells data and append it to your textboxes.

   <asp:GridView ID="yourGrid" runat="server" AutoGenerateColumns="False" 
                    DataSourceID="YourSource" >
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:CheckBox ID="YourCheckBox" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="Name" HeaderText="Name" />
            <asp:BoundField DataField="ModifiedDate" HeaderText="ModifiedDate" />
            <asp:CommandField ShowEditButton="True" />
        </Columns>
    </asp:GridView>

Here is updated Code Behind##

If you are not comfy with JQuery then go with this.

    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow gr = (GridViewRow)chk.Parent.Parent;
txtBox.Text = GridView1.DataKeys[gr.RowIndex].Value.ToString();


} 

REVISION - A JQuery Solution

However, I think the easiest way of doing this based on your requirements is by using JQuery.

First add a jquery script to your project. Go to Jquery.com pull down the latest script if your project does not have it already. In the head of your page mark up add the ref.

<script type="text/javascript" src="The source of your JS file"></script>

Then at the end of your body write your JQuery.

<script type="text/javascript">
    $(document).ready(function () {
    // Capture the click event by going through the gridview > tablerow > tabledisplay >        `lastly the checkbox - grab the click event.
 $('#GrdViewID' tr td  
 input[id*="chkSelected"]`[type=checkbox]:checked').on('click',function () {
          $('#txtBox').text($(this).closest('tr').find('td').eq(2).text());
     });
</script>

这篇关于ASP的GridView,其选择多个行,并检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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