none
我在利用ajax的时候,js里面怎么取到gridview中我选中行的某一个字段的值?请指点!孟兄在否? RRS feed

  • 问题

  • <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="ProjectInfo_MasterPage" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
     <style type="text/css">
            .black_overlay{
                display: none;
                position: absolute;
                top: 0%;
                left: 0%;
                width: 100%;
                height: 100%;
                background-color: white;
                z-index:1001;
                -moz-opacity: 0.8;
                opacity:.80;
                filter: alpha(opacity=80);
            }
            .white_content {
                display: none;
                position: absolute;
                top: 20%;
                left: 20%;
                width: 60%;
                height: 50%;
                padding: 16px;
                border: 2px solid #5D7B9D;
                background-color: white;
                z-index:1002;
                overflow: auto;
               
                }
            .boxCloseButton {
            float:right;
            cursor:pointer;
            padding:6px;
            font-family:Fixedsys;
       
            }   
           
             </style>

    <style type="text/css">
                        .GridViewStyle
                   {  
                        border-right: 2px solid #A7A6AA;
                        border-bottom: 2px solid #A7A6AA;
                        border-left: 2px solid white;
                        border-top: 2px solid white;
                        padding: 4px;
                    }

                    .GridViewStyle a
                    {
                        color: #FFFFFF;
                    }

                    .GridViewHeaderStyle th
                    {
                        border-left: 1px solid #EBE9ED;
                        border-right: 1px solid #EBE9ED;
                    }

                    .GridViewHeaderStyle
                    {
                        background-color: #5D7B9D;
                        font-weight: bold;
                        color: White;
                    }

                    .GridViewFooterStyle
                    {
                        background-color: #5D7B9D;
                        font-weight: bold;
                        color: White;
                    }

                    .GridViewRowStyle
                    {
                        background-color: #F7F6F3;
                        color: #333333;
                    }

                    .GridViewAlternatingRowStyle
                    {
                        background-color: #FFFFFF;
                        color: #284775;
                    }

                    .GridViewRowStyle td, .GridViewAlternatingRowStyle td
                    {
                        border: 1px solid #EBE9ED;
                    }

                    .GridViewSelectedRowStyle
                    {
                        background-color: #E2DED6;
                        font-weight: bold;
                        color: #333333;
                    }

                    .GridViewPagerStyle
                    {
                        background-color: #284775;
                        color: #FFFFFF;
                    }

                    .GridViewPagerStyle table /**//* to center the paging links*/
                    {
                        margin: 0 auto 0 auto;
                    }
                   
        </style>

    以上是母版页里面的内容


    <%@ Page Language="C#" MasterPageFile="../ProjectInfo/MasterPage.master" AutoEventWireup="true"
        CodeFile="searchitem.aspx.cs" Inherits="ProjectInfo_searchitem" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="contentCenter" runat="Server">
        <script type="text/javascript">
      function check()
      { 
         document.getElementById('content').style.display='block';
         document.getElementById('fade').style.display='block'
      }
      function close()
      {
      document.getElementById('content').style.display='none';
      document.getElementById('fade').style.display='none'
      }
      function goal()
      {
      //在这里我怎么能取到选中行的ID中的值(ID这一行的Visible="false")????????,我是想实现弹出层,从中选择项目信息,然后利用ClientScript.GetCallbackEventReference到后台取值,返回到父页面中的控件里去
     
     
       document.getElementById('content').style.display='none';
       document.getElementById('fade').style.display='none'
      }
        </script>
        <div id="content" class="white_content">
            <asp:Label ID="Label1" runat="server" Text="项目名称"></asp:Label>
            <asp:TextBox ID="itemname" runat="server"></asp:TextBox>
            <asp:Label ID="Label2" runat="server" Text="项目类型"></asp:Label>
            <asp:TextBox ID="producttype" runat="server"></asp:TextBox>
            <asp:Label ID="Label5" runat="server" Text="所属地区"></asp:Label>
            <asp:TextBox ID="province" runat="server"></asp:TextBox>
            <asp:Button ID="Button1"   runat="server" Text="确定" OnClick="Click_searchitem" />
            <input id="Button3" type="button" value="关闭" onclick="close();" /> 
            <input id="Button4" type="button" value="关闭" onclick="document.getElementById('content').style.display='none';document.getElementById('fade').style.display='none'" /> 
            <br />
           
              <asp:GridView ID="view_Item" CssClass="GridViewStyle" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                EmptyDataText=" 暂无数据!" Width="700px" DataKeyNames="pro_ID"
                DataSourceID="sql_Item">
                <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
                <RowStyle CssClass="GridViewRowStyle" />
                <HeaderStyle CssClass="GridViewHeaderStyle" />
                <Columns>
                    <asp:BoundField HeaderText="项目名称" HeaderStyle-CssClass="GridViewHeaderStyle" DataField="pro_ItemName" />
                    <asp:BoundField HeaderText="项目类型" HeaderStyle-CssClass="GridViewHeaderStyle" DataField="pro_ProductType" />
                    <asp:BoundField HeaderText="所属地区" HeaderStyle-CssClass="GridViewHeaderStyle" DataField="pro_Province" /> 
                    <asp:BoundField HeaderText="ID" HeaderStyle-CssClass="GridViewHeaderStyle" Visible="false" DataField="pro_ID" />  //我就是想点击这行的时候,从js里面取到这个值!!!!!!!!
                    <asp:TemplateField HeaderText="点击确定">
                        <HeaderStyle CssClass="GridViewHeaderStyle" />
                        <ItemTemplate>
                          <input id="Confirm" type="button" value="选择" onclick="goal();" /> 
                        </ItemTemplate>
                    </asp:TemplateField>             
                </Columns>
            </asp:GridView>
       </div>
        <div id="fade" class="black_overlay">
        </div>
        <asp:Table BorderStyle="Ridge" ID="MainInfo" GridLines="Both" runat="server" BorderWidth="1">
            <asp:TableRow>
                <asp:TableCell Width="90px">
                    <asp:Label ID="Label3" Width="95%" runat="server" Text="项目名称:"></asp:Label></asp:TableCell>
                <asp:TableCell HorizontalAlign="Left" ColumnSpan="3" Width="380px">
                    <asp:TextBox ID="BidUser" Width="50%" runat="server"></asp:TextBox>
                    <input id="Button2" type="button" value="点击选择项目" onclick="check();" />
                </asp:TableCell>
                <asp:TableCell Width="90px">
                    <asp:Label runat="server" Width="95%" ID="label4" Text="项目编号:"></asp:Label></asp:TableCell>
                <asp:TableCell Width="150px">
                    <asp:TextBox ID="TransItemCode" Width="95%" runat="server"></asp:TextBox></asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell Width="90px">
                    <asp:Label ID="Label43" Width="95%" runat="server" Text="项目类型:"></asp:Label></asp:TableCell>
                <asp:TableCell Width="150px">
                    <asp:TextBox ID="TransDiffer" Width="95%" runat="server"></asp:TextBox></asp:TableCell>
                <asp:TableCell Width="90px">
                    <asp:Label ID="Label49" Width="95%" runat="server" Text="所属地区:"></asp:Label></asp:TableCell>
                <asp:TableCell ColumnSpan="3" HorizontalAlign="Left" Width="380px">
                    <asp:TextBox ID="Copies" Width="95%" runat="server"></asp:TextBox></asp:TableCell>
            </asp:TableRow>
        </asp:Table>
       
       
         <asp:SqlDataSource ID="sql_Item" ConnectionString="<%$ ConnectionStrings:myConnection %>"
            SelectCommand="Select * From pro_Main"
            runat="server" />
    </asp:Content>

    后台文件
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class ProjectInfo_searchitem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string cmd;
            cmd = itemname.Text;
        }
        protected void Click_searchitem(object obj, EventArgs e)
        {
            string str_itemname, str_producttype, str_province;
            str_itemname = itemname.Text;
            str_producttype = producttype.Text;
            str_province = province.Text;
            string cmd = "Select * From pro_Main ";
            string cmd1 = "";
            if (str_itemname != "")
            {
                if (cmd1 + "" != "")
                    cmd1 = cmd1 + " and pro_ItemName like '%" + str_itemname + "%'";
                else
                    cmd1 = cmd1 + "pro_ItemName like '%" + str_itemname + "%'";
            }
            if (str_producttype != "")
            {
                if (cmd1 + "" != "")
                    cmd1 = cmd1 + " and pro_ProductType like '%" + str_producttype + "%'";
                else
                    cmd1 = cmd1 + "pro_ProductType like '%" + str_producttype + "%'";
            }
            if (str_province != "")
            {
                if (cmd1 + "" != "")
                    cmd1 = cmd1 + " and pro_Province like '%" + str_province + "%'";
                else
                    cmd1 = cmd1 + "pro_Province like '%" + str_province + "%'";
            }
            if (cmd1 != "")
                cmd = cmd + " where " + cmd1 + "Order By pro_ID";
            else
                cmd = cmd + "Order By pro_ID";

            sql_Item.SelectCommand = cmd;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alertScript", "check()", true);
        }
    }

    2009年4月17日 7:51

答案

全部回复

  •   <asp:GridView ID="view_Item" CssClass="GridViewStyle" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                EmptyDataText=" 暂无数据!" Width="700px" DataKeyNames="pro_ID"
                DataSourceID="sql_Item">
                <AlternatingRowStyle CssClass="GridViewAlternatingRowStyle" />
                <RowStyle CssClass="GridViewRowStyle" />
                <HeaderStyle CssClass="GridViewHeaderStyle" />
                <Columns>
                    <asp:BoundField HeaderText="项目名称" HeaderStyle-CssClass="GridViewHeaderStyle" DataField="pro_ItemName" />
                    <asp:BoundField HeaderText="项目类型" HeaderStyle-CssClass="GridViewHeaderStyle" DataField="pro_ProductType" />
                    <asp:BoundField HeaderText="所属地区" HeaderStyle-CssClass="GridViewHeaderStyle" DataField="pro_Province" /> 
                    <asp:BoundField HeaderText="ID" HeaderStyle-CssClass="GridViewHeaderStyle" Visible="false" DataField="pro_ID" />  //我就是想点击这行的时候,从js里面取到这个pro_ID值!!!!!!!!
                    <asp:TemplateField HeaderText="点击确定">
                        <HeaderStyle CssClass="GridViewHeaderStyle" />
                        <ItemTemplate>
                          <input id="Confirm" type="button" value="选择" onclick="goal();" />  //我在点击选择的时候,怎么从js里面取到上面一行的pro_ID值
                        </ItemTemplate>
                    </asp:TemplateField>             
                </Columns>
            </asp:GridView>
    2009年4月17日 8:19
  • gridview的最终html就是table,按照DOM操作就可以了
    孟宪会
    2009年4月17日 9:07
    版主
  • onclick="goal('<%#Eval("pro_ID")%>');" />
    直接赋值为参数

    孟宪会
    2009年4月17日 9:09
    版主
  • 对你的景仰真是没话说了!
    2009年4月17日 9:29