简体   繁体   中英

GridView Order of Operations

No error as such, but the way I have it setup, the GridView renders on Page_Load, it would seem that then the rest of operations that I have setup don't seem to fire.

Any ideas on the order? Been at it for hours now :(

<asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" EmptyDataText="No Records Found" AutoGenerateColumns="False" DataSourceID="showOrders" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Height="74px" Width="394px">
<Columns>
<asp:TemplateField HeaderText ="Pizza Type" SortExpression="pizza_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("pizza_id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mylabel" runat="server" Text='<%#Bind("pizza_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="quantity" HeaderText="Quanity" SortExpression="quantity" />
<asp:BoundField DataField="pizza_size" HeaderText="Pizza Size" SortExpression="pizza_size" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>

<asp:SqlDataSource ID="showOrders" runat="server" ConnectionString="<%$ ConnectionStrings:Database %>"></asp:SqlDataSource>

Code Behind:

public partial class order_details : System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        string username = User.Identity.Name;
        showOrders.SelectParameters.Add("username", username);
        showOrders.SelectCommand = "SELECT [pizza_id], [quantity], [pizza_size] FROM [orders] WHERE ([username]=@username)";
     }

    protected void GridView1_Databound(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
           row.Visible = row.RowIndex.Equals(1);
        }
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            string value = e.Row.Cells[7].Text;

            Label myLabel = (Label) e.Row.FindControl("myLabel");
            if (value == "1")
            {
                myLabel.Text = "Big Cheese";
            }
            else if (value == "2")
            {
                myLabel.Text = "BBQ Beef";
            }
            else if (value == "3")
            {
                myLabel.Text = "Chicken and Pineapple";
            }
            else if (value == "4")
            {
                myLabel.Text = "Pepperoni Feast";
            }
            else if (value == "5")
            {
                myLabel.Text = "Vegetarian";
            }
    }

Any help would be appreciated.

Thanks Kindly.

You need to wire up the offending events:

<asp:GridView ID="GridView1" runat="server" 
  ShowHeaderWhenEmpty="True"
  EmptyDataText="No Records Found" 
  AutoGenerateColumns="False" 
  DataSourceID="showOrders" 
  OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
  BackColor="#DEBA84"
  BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 
  CellPadding="3" CellSpacing="2" 
  Height="74px" Width="394px" 
  OnRowDataBound="GridView1_RowDataBound"
  OnDataBound="GridView1_Databound">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM