0

根据按下哪个按钮,我必须获取下一个表单中需要的 excursion_date_id!

所以最好使用什么 - 也许是 buttonfield 属性!

这是我的代码

首先是 Dates.class

public class Dates
   {
        private int excursionID;
        private DateTime startdates;
        private double prices;
    public  int ExcursionID
    {
        get { return excursionID; }
        set { excursionID = value; }

    }
    public DateTime StartDates
    {
        get { return startdates; }
        set { startdates = value; }

   }

    public  double Prices
    {
        get { return prices; }
        set { prices = value; }
    }


    public Dates()
    { }


   }

}

然后我用我的数据库中的日期填充gridview 我想在gridview 的每一行的末尾有一个带有文本属性“reserve”的按钮!

protected void Page_Load(object sender, EventArgs e)
        {
           string excursionnId = Request.QueryString["ExcursionId"];


        Dates date = new Dates();
        List<Dates> listDate = new List<Dates>();
        listDate = GetDates();
        gvDates.DataSource = listDate;
        gvDates.DataBind();

    }

    public List<Dates> GetDates()
    {
        List<Dates> datesList = new List<Dates>();

        string connectionString = "Server=localhost\\SQLEXPRESS;Database=EXCURSIONSDATABASE;Trusted_Connection=true";
        string excursionnID = Request.QueryString["ExcursionID"];
        string query =
    "SELECT Excursion_date_ID, Excursion_ID, Start_date, Price FROM EXCURSION_DATES WHERE EXCURSION_ID='" + excursionnID + "'";
        SqlConnection conn = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand(query, conn);
        try
        {
            conn.Open();
            SqlDataReader rd = cmd.ExecuteReader();
            int s=0;
            while (rd.Read())
            {
                Dates dates = new Dates();

                dates.ExcursionID = Convert.ToInt32(rd["Excursion_ID"]);
                dates.StartDates = Convert.ToDateTime(rd["Start_date"]);
                dates.Prices = Convert.ToDouble(rd["Price"]);
                datesList.Add(dates);

            }
        }
        catch (Exception EX)
        {

        }
        return datesList;
    }
}
4

2 回答 2

1
 <asp:GridView ID="gvDates" runat="server" Width="100%" AutoGenerateColumns="false"  
                    OnRowCommand="grd_RowCommand" >
                    <Columns>
                        <asp:TemplateField>
                              <ItemTemplate>
                                 <asp:Button ID="btn" CausesValidation="false"    CommandName="YourCommandName" 
                         CommandArgument='<%#Eval("Excursion_ID") %>' runat="server" Text="Text" />

                              </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView> 
protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        string number = (string)e.CommandArgument;
        if (number==null) return;
        switch (e.CommandName)
        {
          case "YourCommandName":
                Load(number);
                break;

        // some others command
         }
    }

或不带行命令

 <asp:GridView ID="gvDates" runat="server" Width="100%" AutoGenerateColumns="false"  >
                <Columns>
                    <asp:TemplateField>
                          <ItemTemplate>
                             <asp:Button ID="btn" CausesValidation="false"     OnClick="btnClick" 
                     CommandArgument='<%#Eval("Excursion_ID") %>' runat="server"  Text="Text" />

                          </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView> 
protected void btnClick(object sender, EventArgs e)
{
    Button btn=(Button)sender;
    if(btn==null) return;
    string number = (string)btn.CommandArgument;
    if (number==null) return;

    Load(number);


}
于 2012-11-29T08:51:05.423 回答
0

在您的 .aspx 页面中,将 GridView 按钮的 ID 指定为 ExcursionID。您可以为此使用按钮字段或在模板列中放置一个按钮。

于 2012-11-29T08:49:00.727 回答