簡體   English   中英

單擊gridview按鈕后,從列和行中讀取數據

[英]Read data from column and row after clicking gridview button

我有一個gridview,其中每個生成的行都有gridview按鈕。 當我單擊按鈕時,它將向用戶發送電子郵件,但我需要能夠讀取列(WHERE列)及其行值之一,並將其​​包含在消息正文中

這是我的標記代碼

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"        onrowcommand="GridView1_RowCommand"
    DataSourceID="prmtest" Width="994px" BorderStyle="None" GridLines="None" 
    onselectedindexchanged="GridView1_SelectedIndexChanged" 
    HorizontalAlign="Center">
    <RowStyle ForeColor="#4D5763" HorizontalAlign="Center" />
    <Columns>
        <asp:BoundField DataField="Name_of_Training" HeaderText="Name of training" 
            SortExpression="Name_of_Training" />
        <asp:BoundField DataField="Column1" HeaderText="Date" ReadOnly="True" 
            SortExpression="Column1" />
        <asp:BoundField DataField="Start_time" HeaderText="Start time" 
            SortExpression="Start_time" />
        <asp:BoundField DataField="End_time" HeaderText="End time" 
            SortExpression="End_time" />
        <asp:BoundField DataField="Location" HeaderText="Where" 
            SortExpression="Location" />
        <asp:BoundField DataField="Seats" HeaderText="Available seats" SortExpression="Seats" />
    <asp:HyperLinkField HeaderText="Training documents" 
        NavigateUrl="url.zip" Text="Training material" Target="_self">
        <ItemStyle ForeColor="#E5003B"    Font-Underline="false"/>
    </asp:HyperLinkField>
    <asp:TemplateField ShowHeader="False">
        <ItemTemplate>
            <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="Book"
             OnClientClick="alert('The booking will be made for?')" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"
                Text="Book" />
        </ItemTemplate>
            <ControlStyle Font-Bold="False" />
    </asp:TemplateField>
    </Columns>
    <HeaderStyle ForeColor="#4D5763" BackColor="#DCDFE2" />
    <EditRowStyle Font-Underline="False" />
</asp:GridView>
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
     <asp:SqlDataSource ID="prmtest" runat="server" 
         ConnectionString="<%$ ConnectionStrings:dbprmtest %>" 
         SelectCommand="SELECT [Name of Training] AS Name_of_Training, 
         CONVERT(VARCHAR(10),[Date],20), [Start time] AS Start_time, [End time] AS End_time, [Location], [Seats] FROM [Sheet1$]">
     </asp:SqlDataSource>

這是我的電子郵件代碼

    if (e.CommandName == "Book")
    {
        try
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add("email@test.com");
            mailMessage.From = new MailAddress("email2@test.com");
            mailMessage.Subject = "Test";
            mailMessage.Body = "";
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = "smtp.text.com;
            smtpClient.Host = "test.com";
            smtpClient.Port = 25;
            smtpClient.Credentials = new NetworkCredential("test.test", "password");
            smtpClient.Send(mailMessage);
            btnemail.Text = "E-mail  and Booking sent!";
        }
        catch (Exception ex)
        {
            Response.Write("Could not send the e-mail - error: " + ex.Message);
            lblemail.Text = "Could not send the e-mail - error: " + ex.Message;
        }
    }
}

在“ oncommand”事件處理程序中,首先找到包含“到”按鈕的網格行的“所有者”

Button btn = (sender as Button);
GridViewRow grv = (GridViewRow)btn.NamingContainer;

然后,GridRowView使您可以訪問基礎數據源中的行(DataItemIndex),然后可以從下面使用中拉出任何/所有字段

DataView dview = (DataView)prmtest.Select(DataSourceSelectArguments.Empty);
string str = (string)dview[grv.DataItemIndex]["Location"];

另外,如果實際上只是您需要的位置,那么也許就這么簡單。

Button btn = (sender as Button);
GridViewRow grv = (GridViewRow)btn.NamingContainer;

string str = gvAssets.Rows[grv.DataItemIndex].Cells[4].Text;   // Cell[4] being the Location Col.

嗯...

有兩種選擇:

第一種方法是在CommandArguement中發送所需的數據。 喜歡:

CommandArgument='<%#((GridViewRow)Container).RowIndex.ToString() +"|" + Eval("Anything").ToString() %>'

| 符號在此處用作分隔符。 然后,您可以使用分隔符來分割字符串,例如:

var arguements = CommandArguement.ToString().Split('|');

第二種方法是使用行索引轉到該行,並搜索您想要在郵件中包含的元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM