6

我有一个连接到显示数据的 SQL 数据源的 Gridview,我希望做的是通过按钮和 Eval 检索与选定行关联的数据。

有点像,

<asp:LinkButton runat=server OnClientClick="RetrieveInfo" Text="Send" />

但是我不能从代码中调用 Eval,我也不知道如何获取 DataKey。

我一直在网上搜索,但没有找到任何好的东西。谁能帮我?将不胜感激。

编辑:

<asp:GridView ID="GridView1" Width="100%" runat="server" AutoGenerateColumns="False"
DataKeyNames="ScheduleID" DataSourceID="SqlDataSource1">
    <Colums>
        <asp:TemplateField HeaderText="Date" SortExpression="Date">
           <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("Date")%>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Time" SortExpression="starttime">
           <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%#Eval("starttime")%>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
           <ItemTemplate>
            <asp:LinkButton runat=server OnClientClick="RetrieveInfo" Text="Send" />
          </ItemTemplate>
        </asp:TemplateField>
    </Colums>
</asp:GridView>

它是略读的,但基本上就是这样。

4

3 回答 3

3

愿这有帮助

将您的gridview html编辑<Colums></Columns>

<asp:LinkButton runat=server OnClientClick="RetrieveInfo"   CommandName="Update"  Text="Send" />

网格视图 HTML

<asp:GridView ID="GridView1" Width="50%" runat="server" AutoGenerateColumns="False" 
            onrowcommand="GridView1_RowCommand" >
   <Columns>
        <asp:TemplateField HeaderText="Date" SortExpression="Date">
           <ItemTemplate>
            <asp:Label ID="lbldate" runat="server" Text='<%#Eval("Date")%>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Time" SortExpression="starttime">
           <ItemTemplate>
            <asp:Label ID="lbltime" runat="server" Text='<%#Eval("starttime","{0:dd MMM yyyy}") %>' />
          </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
           <ItemTemplate>
            <asp:LinkButton runat="server" CommandName="sendvalue" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClientClick="RetrieveInfo" Text="Send" />
          </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

代码背后:

     protected void Page_Load(object sender, EventArgs e)
       {
           bindGridview();
       }

   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "sendvalue")
            {
                int getrow = Convert.ToInt32(e.CommandArgument);
                Label lbldate = (Label)GridView1.Rows[getrow].FindControl("lbldate");
                Label lbltime = (Label)GridView1.Rows[getrow].FindControl("lbltime");
                string getDate = lbldate.Text;
                string getStartTime = lbltime.Text;
               //here you retrieve all the value of select row and do your logic for link butn
                GridView1.EditIndex = -1;
                bindGridview();
            }
    }

    public void bindGridview()
    {
        SqlDataAdapter dap = new SqlDataAdapter("select Date,startTime from yourtable", con);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        DataTable dt = ds.Tables[0];
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
于 2012-08-09T06:07:13.463 回答
0

从您的代码中,我觉得您需要在 Button OnClientClick 事件的 Javascript 函数调用中检索这些值。您可以在 GridView 的 RowDataBound 事件中为按钮的 onclick 事件设置 Javascript 函数中的值,并且可以轻松编写 javascript获取这些值的函数...

代码隐藏:-

void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
 {

   if(e.Row.RowType == DataControlRowType.DataRow)
   {
    Label Label1 = (Label)e.Row.FindControl("Label1Id");                    
    Label Label2= (Label)e.Row.FindControl("Label2Id");
    Label Label3= (Label)e.Row.FindControl("Label3Id");
    LinkButton lnkBtn= (LinkButton)e.Row.FindControl("lnkId");
    lnkBtn.Attributes.Add("onclick","RetrieveInfo('"+Label1.Text+"','"+Label2.Text+"','"+Label3.Text+"')");
   }

  }

Javascript:

<script>
 function RetrieveInfo(Label1Value,Label2Value,Label3Value)
{
   Write your Logic..
}
</script>
于 2012-08-09T06:26:30.380 回答
0

如下更改您的网格视图。添加行数据绑定和行命令功能。当您单击链接按钮时,将调用行命令函数。

默认.aspx

<asp:GridView ID="GridView1" Width="100%" runat="server" AutoGenerateColumns="False"
    DataKeyNames="ScheduleID" DataSourceID="SqlDataSource1" 
    OnRowDataBound = "GridView1_OnRowDataBound" 
    onrowcommand="GridView1_RowCommand" >
        <Colums>
            <asp:TemplateField HeaderText="Date" SortExpression="Date">
               <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("Date")%>' />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Time" SortExpression="starttime">
               <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%#Eval("starttime")%>' />
              </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
               <ItemTemplate>
                <asp:LinkButton ID="lbBind" runat=server OnClientClick="RetrieveInfo" Text="Send" />
              </ItemTemplate>
            </asp:TemplateField>
        </Colums>
    </asp:GridView>

.cs 后面的代码:

protected void  GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e )
  {
     try
       { if (e.Row.RowType == DataControlRowType.DataRow)
           {
             DataRowView drEachRow = (DataRowView)e.Row.DataItem;
            // Get all controls present in the row.
            LinkButton lbBind= (LinkButton )e.Row.FindControl("lbBind");
            // Add row index as command argument.
            lbBind.CommandArgument = e.Row.RowIndex.ToString();
            }
         }
           catch(Exception objExp)
              {
                 // handle exception
              }
            }

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
      try
       {   // Check if the command is generated by a button only.
         if (e.CommandSource.GetType().Equals(new LinkButton().GetType()))
         {
             int index;          // Store the index of the current row.
             // Get it from CommandArgument
             int.TryParse(Convert.ToString(e.CommandArgument), out index);
            // One exacmple of how to get one control in the same row.
           Label lblpid = (Label)gvFinalReview.Rows[index].FindControl("lblplanid");
          // You can get other controls similarly.
         }
       }
        catch(Exception objExp)
         {
            // handle exception
          }
    }
于 2012-08-09T06:34:56.130 回答