0

我有一个小问题!一旦用户从下拉列表中选择了一个特定的名称,我就编写了一个代码来绑定三个网格视图......在每个网格视图中,我为用户提供了一个选项来编辑和更新网格视图中的特定行。 .问题是页面加载时...gridview数据没有显示...但是如果我单击特定行的编辑按钮..它会显示数据。你们能帮帮我吗?下面是我的代码。

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;
using System.Data.SqlClient;
namespace Travel1.Forms
{
    public partial class claimadmin : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(""); 
        SqlDataAdapter dadapter;
        DataSet dset;
        string sql = "SELECT DISTINCT M_Emp_Personal.EmpName, T_TADA_tempform.req_ID FROM     T_TADA_tempform INNER JOIN M_Emp_Personal ON T_TADA_tempform.EmpID = M_Emp_Personal.EmpID INNER JOIN T_TADA_claim ON T_TADA_tempform.req_ID = T_TADA_claim.req_ID";
        protected void Page_Load(object sender, EventArgs e)
        {
             if (!Page.IsPostBack)
            {

                Lbltoday.Text = DateTime.Now.ToString();

                dadapter = new SqlDataAdapter(sql, conn);
                dset = new DataSet();
                dadapter.Fill(dset);
                ddlname.DataSource = dset.Tables[0];
                ddlname.DataTextField = "EmpName";
                ddlname.DataValueField = "req_ID";
                ddlname.DataBind();
                ddlname.Items.Insert(0, "--Select--");
            }
        }
         protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label3.Visible = true;
            Label4.Visible = true;
            txtrma.Visible = true;
            txtda.Visible = true;
            int empid = Convert.ToInt32(ddlname.SelectedValue);
            Gridview1Bind();
            Gridview2Bind();
             Gridview3Bind();

        }
         protected void Gridview1Bind()
        {

                conn.Open();
                SqlCommand cmd = new SqlCommand("SELECT place_arrived, arrival_date, arrival_time, distance, mode, departure_date, departure_time, amount, ticket_number, total_amount FROM T_TADA_journey_details where req_ID=@req_ID", conn);
                cmd.Parameters.AddWithValue("@req_ID",ddlname.SelectedValue);
                SqlDataReader dr = cmd.ExecuteReader();
                GridView1.DataSource = dr;
                GridView1.DataBind();
                conn.Close();
            }
         protected void gv1Details_RowEditing(object sender, GridViewEditEventArgs e)
         {
             GridView1.EditIndex = e.NewEditIndex;
             Gridview1Bind();
         }
         protected void gv1Details_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {
             TextBox txtplace = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtplace");
             TextBox txtadate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtadate");
             TextBox txtatime = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtatime");
             TextBox txtdistance = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdistance");
             TextBox txtmode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmode");
             TextBox txtddate = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtddate");
             TextBox txtdtime = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdtime");
             TextBox txtamt = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtamt");
             TextBox txtticket = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtticket");
             TextBox txttotamt = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txttotamt");
             conn.Open();
             SqlCommand cmd = new SqlCommand("update T_TADA_journey_details set place_arrived='"+ txtplace.Text+"',arrival_date='"+ txtadate.Text+"',arrival_time='"+ txtatime.Text+"',distance='"+ txtdistance.Text+"', mode='"+ txtmode.Text+"',departure_date='"+ txtddate.Text+"',departure_time='"+ txtdtime.Text+"',amount='"+ txtamt.Text+"',ticket_number='"+ txtticket.Text+"', total_amount='"+ txttotamt.Text+"' where req_ID=" + ddlname.SelectedValue , conn);
             cmd.ExecuteNonQuery();
             conn.Close();
             GridView1.EditIndex = -1;
             Gridview1Bind();
         }
         protected void Gridview2Bind()
         {
             conn.Open();
             SqlCommand cmd = new SqlCommand("SELECT from_place, to_place, distance, amount_paid, total_amount, total_sum_amount FROM     T_TADA_RMA where req_ID=@req_ID", conn);
             cmd.Parameters.AddWithValue("@req_ID", ddlname.SelectedValue);
             SqlDataReader dr = cmd.ExecuteReader();
             GridView2.DataSource = dr;
             GridView2.DataBind();
             conn.Close();
         }
         protected void gv2Details_RowEditing(object sender, GridViewEditEventArgs e)
         {
             GridView2.EditIndex = e.NewEditIndex;
             Gridview2Bind();
         }
         protected void gv2Details_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {
             TextBox txt1 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtfplace");
             TextBox txt2 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txttoplace");
             TextBox txt3 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtdistance");
             TextBox txt4 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtamount_paid");
             TextBox txt5 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txttotal_amount");
             TextBox txt6 = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txttotal_sum_amount");

             conn.Open();
             SqlCommand cmd1 = new SqlCommand("update T_TADA_RMA set from_place='" + txt1.Text + "',to_place='" + txt2.Text + "',distance='" + txt3.Text + "',amount_paid='" + txt4.Text + "', total_amount='" + txt5.Text + "',total_sum_amount='" + txt6.Text + "'  where req_ID=" + ddlname.SelectedValue, conn);

             cmd1.ExecuteNonQuery();
             conn.Close();
             GridView2.EditIndex = -1;
             Gridview2Bind();
         }
         protected void Gridview3Bind()
         {
             conn.Open();
             SqlCommand cmd = new SqlCommand("SELECT total_amount, advance_given, recieved_amount FROM     final_amount_claim where req_ID=@req_ID", conn);
             cmd.Parameters.AddWithValue("@req_ID",ddlname.SelectedValue);
             SqlDataReader dr = cmd.ExecuteReader();
             GridView3.DataSource = dr;
             GridView3.DataBind();
             conn.Close();
         }
         protected void gv3Details_RowEditing(object sender, GridViewEditEventArgs e)
         {
             GridView3.EditIndex = e.NewEditIndex;
             Gridview3Bind();
         }
         protected void gv3Details_RowUpdating(object sender, GridViewUpdateEventArgs e)
         {
             TextBox txt1 = (TextBox)GridView3.Rows[e.RowIndex].FindControl("txttotalamt");
             TextBox txt2 = (TextBox)GridView3.Rows[e.RowIndex].FindControl("txtadvance");
             TextBox txt3 = (TextBox)GridView3.Rows[e.RowIndex].FindControl("txtrecieved");

             conn.Open();
             SqlCommand cmd1 = new SqlCommand("update final_amount_claim set total_amount='" + txt1.Text + "',advance_given='" + txt2.Text + "',recieved_amount='" + txt3.Text + "'  where req_ID=" + ddlname.SelectedValue, conn);
             cmd1.ExecuteNonQuery();
             conn.Close();
             GridView3.EditIndex = -1;
             Gridview3Bind();
         }
         protected void btnOK_Click(object sender, EventArgs e)
         {
             conn.Open();
                 SqlCommand cmd=new SqlCommand("Update final_amount_claim set no_da=@no_da,rate_rma=@rate_rma where req_ID=@req_ID",conn);
             cmd.Parameters.AddWithValue("no_da",txtda.Text);
             cmd.Parameters.AddWithValue("rate_rma",txtrma.Text);
             cmd.Parameters.AddWithValue("req_ID",ddlname.SelectedValue);
             cmd.ExecuteNonQuery();
             lblmsg.Visible = true;
             conn.Close();
         }
        }
    }

*

下面是 ASP.NET 代码

    *
<asp:GridView ID="GridView1" runat="server"  CssClass="vutblrow" TabIndex="6" onrowediting="gv1Details_RowEditing" onrowupdating="gv1Details_RowUpdating"                                                                        CellPadding="4" ForeColor="#333333" GridLines="None"  Width="18%"  
PagerStyle-Mode="NumericPages" HeaderStyle-BackColor="black" 
HeaderStyle-ForeColor="White" Height="53px" AutoGenerateColumns="false"    > 
<HeaderStyle BackColor="Black" ForeColor="White"></HeaderStyle>
<PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />
<Columns>
<asp:TemplateField HeaderText="Place Arrived">
<EditItemTemplate>
<asp:TextBox ID="txtplace" runat="server" Text='<%#Eval("place_arrived") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Arrival Date">
<EditItemTemplate>
<asp:TextBox ID="txtadate" runat="server" Text='<%#Eval("arrival_date") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Arrival Time">
<EditItemTemplate>
<asp:TextBox ID="txtatime" runat="server" Text='<%#Eval("arrival_time") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Distance">
<EditItemTemplate>
<asp:TextBox ID="txtdistance" runat="server" Text='<%#Eval("distance") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mode">
<EditItemTemplate>
<asp:TextBox ID="txtmode" runat="server" Text='<%#Eval("mode") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Departure Date">
<EditItemTemplate>
<asp:TextBox ID="txtddate" runat="server" Text='<%#Eval("departure_date") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Departure Time">
<EditItemTemplate>
<asp:TextBox ID="txtdtime" runat="server" Text='<%#Eval("departure_time") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<EditItemTemplate>
<asp:TextBox ID="txtamt" runat="server" Text='<%#Eval("amount") %>'/>
</EditItemTemplate>
</asp:TemplateField>                                                                        <asp:TemplateField HeaderText="Ticket number">
<EditItemTemplate>                                                                      <asp:TextBox ID="txtticket" runat="server" Text='<%#Eval("ticket_number") %>'/>
</EditItemTemplate>
</asp:TemplateField>                                                                        <asp:TemplateField HeaderText="Total Amount">
<EditItemTemplate>
<asp:TextBox ID="txttotamt" runat="server" Text='<%#Eval("total_amount") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgedit" CommandName="Edit" runat="server"ImageUrl="~/Images/edit.gif" />            
</ItemTemplate>  

                                                                             </asp:TemplateField>


                                                                           <asp:TemplateField>
                                                                         <ItemTemplate>
                                                                          <asp:ImageButton ID="imgupdate" CommandName="Update" runat="server" ImageUrl="~/Images/Update.gif" />
                                                                           </ItemTemplate>  

                                                                             </asp:TemplateField>

                                                                         </Columns>
                    </asp:GridView>
                                                                     &nbsp;</td>
                                                                <tr>
                                                                <td class="style14" colspan="3">
                 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                 CssClass="vutblrow" TabIndex="6" onrowediting="gv2Details_RowEditing" onrowupdating="gv2Details_RowUpdating"  
                                                                        CellPadding="4" ForeColor="#333333" GridLines="None"  Width="13%"  
                                                                        PagerStyle-Mode="NumericPages" HeaderStyle-BackColor="black" 
                                                                        HeaderStyle-ForeColor="White" Height="77px" > 
                                                                 <HeaderStyle BackColor="Black" ForeColor="White" />
                                                                 <PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />
                                                                  <Columns>
                                                                      <asp:TemplateField HeaderText="From Place">
                                                                      <EditItemTemplate>
                                                                      <asp:TextBox ID="txtfplace" runat="server" Text='<%#Eval("from_place") %>'/>
                                                                        </EditItemTemplate>
                                                                      </asp:TemplateField>
                                                                        <asp:TemplateField HeaderText="To Place">
                                                                      <EditItemTemplate>
                                                                      <asp:TextBox ID="txttoplace" runat="server" Text='<%#Eval("to_place") %>'/>
                                                                        </EditItemTemplate>
                                                                      </asp:TemplateField>
                                                                        <asp:TemplateField HeaderText="Distance">
                                                                      <EditItemTemplate>
                                                                      <asp:TextBox ID="txtdistance" runat="server" Text='<%#Eval("distance") %>'/>
                                                                        </EditItemTemplate>
                                                                      </asp:TemplateField>
                                                                        <asp:TemplateField HeaderText="Amount Paid">
                                                                      <EditItemTemplate>
                                                                      <asp:TextBox ID="txtamount_paid" runat="server" Text='<%#Eval("amount_paid") %>'/>
                                                                        </EditItemTemplate>
                                                                      </asp:TemplateField>
<asp:TemplateField HeaderText="Total Amount"><EditItemTemplate>
<asp:TextBox ID="txttotal_amount" runat="server" Text='<%#Eval("total_amount") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
HeaderText="Sum Of Amount">
<EditItemTemplate>
<asp:TextBox ID="txttotal_sum_amount" runat="server" Text='<%#Eval("total_sum_amount") %>'/>                                                                        </EditItemTemplate>
</asp:TemplateField>                                                    
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgedit" CommandName="Edit" runat="server" ImageUrl="~/Images/edit.gif" /></ItemTemplate>  
<asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgupdate" CommandName="Update" runat="server" ImageUrl="~/Images/Update.gif" />
</ItemTemplate>                                                                            </asp:TemplateField>
</Columns>
</asp:GridView>
&nbsp;</td>
<tr>
<td class="style14" colspan="3">
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" 
CssClass="vutblrow" TabIndex="6" onrowediting="gv3Details_RowEditing" onrowupdating="gv3Details_RowUpdating" 
CellPadding="4" ForeColor="#333333" GridLines="None"  Width="7%"  
PagerStyle-Mode="NumericPages" HeaderStyle-BackColor="black" 
HeaderStyle-ForeColor="White" Height="60px" > 
<HeaderStyle BackColor="Black" ForeColor="White" />
<PagerStyle CssClass="pgr"  Height="25px" BorderStyle="Solid" />
<Columns>
<asp:TemplateField HeaderText="Total Amount">
<EditItemTemplate>
<asp:TextBox ID="txttotalamt" runat="server" Text='<%#Eval("total_amount") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Advance Given">
<EditItemTemplate>
<asp:TextBox ID="txtadvance" runat="server" Text='<%#Eval("advance_given") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Recieved Amount">
<EditItemTemplate>
<asp:TextBox ID="txtrecieved" runat="server" Text='<%#Eval("recieved_amount") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgedit" CommandName="Edit" runat="server" ImageUrl="~/Images/edit.gif" />
</ItemTemplate>  
</asp:TemplateField>                                                                     
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgupdate" CommandName="Update" runat="server" ImageUrl="~/Images/Update.gif" />
</ItemTemplate>  

                                                                         </asp:TemplateField>
</Columns>
</asp:GridView>
*
4

2 回答 2

0

使用 session 保存绑定值,从而保护它以供下次刷新

 Session["DTable"] = dt;

然后调用它来分配它

于 2013-11-10T16:50:03.177 回答
0

在您的Page_Load方法中,我没有看到您进行任何数据网格绑定。ddlname_SelectedIndexChanged(null, null)在绑定下拉列表后,您可能希望显式调用。

于 2013-11-10T17:06:11.263 回答