2017-03-29 18 views
0

私は自分のプロジェクトで作業していましたが、グリッドビューにはボタンがあるステータスフィールドがあります。 ユーザーがそのボタンをクリックすると、そのボタンのテキストが保留中から確認のために変更されます。 問題は、ブラウザを閉じてビジュアルスタジオから.aspxページを再度読み込んだときに、ボタンのテキストが再び保留に設定されてしまうことです。 ボタンがクリックされたときにのみ確認するボタンテキストを保持する方法を教えてもらえます。また、最初の行のステータスフィールドボタンをクリックすると、残りのステータスフィールドがデータベースのステータスを確認して更新されます。 はここに私のコードです。(フロントエンドコード)ボタンのテキストをクリックして変更しても、ボタンのテキストは変更されないようにしたい

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" 
    Font-Bold="True" Font-Size="Small" GridLines="None"> 
    <Columns> 
     <asp:BoundField ItemStyle-Width="150px" DataField="packagename" HeaderText="Package" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="Name" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="gender" HeaderText="Gender" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="mobileno" HeaderText="Mobile No" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="email" HeaderText="Email" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="noofdays" HeaderText="No. of Days" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="child" HeaderText="No. of Children" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="adults" HeaderText="No of Adults" /> 
     <asp:TemplateField HeaderText="Status Field"> 
      <ItemTemplate> 
       <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="MYCOMMAND" 
        Text="Pending" BorderStyle="None" Font-Bold="True" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

(バックエンドコード):

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     List<string> lst = new List<string>() { "asd", "xxx" }; 
     GridView1.DataSource = lst; 
     this.BindGrid(); 
    } 


} 
protected void btnpreviewwebsite_Click1(object sender, EventArgs e) 
{ 
    Response.Redirect("~/index.aspx"); 
} 
protected void btnlogout_Click(object sender, EventArgs e) 
{ 
    Session.Abandon(); 
    Session.Clear(); 
    Response.Redirect("~/Admin Panel/LoginForm.aspx"); 

} 
private void BindGrid() 
{ 

    using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True")) 
    { 
     using (SqlCommand cmd = new SqlCommand("SELECT packagename,name, gender,mobileno,email,noofdays,child,adults FROM enquiry")) 
     { 
      using (SqlDataAdapter sda = new SqlDataAdapter()) 
      { 
       cmd.Connection = con; 
       sda.SelectCommand = cmd; 
       using (DataTable dt = new DataTable()) 
       { 
        sda.Fill(dt); 
        GridView1.DataSource = dt; 
        GridView1.DataBind(); 
       } 
      } 
     } 
    } 
} 
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 



    if (e.CommandName == "MYCOMMAND") 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True"); 
     string sql; 
     Button Button1 = (Button)e.CommandSource; 
     if (Button1 != null) 
      Button1.Text = "Confirm"; 
     sql = "update enquiry set statusfield='" + Button1.Text + "'"; 

     SqlCommand comm = new SqlCommand(sql, con); 

     con.Open(); 
     comm.ExecuteNonQuery(); 

    } 
} 

答えて

0
あなたのボタンのテキストをDBから取得されますので、

あなたはasp.netからバインド機能を使用することができます結果。以下のように。それはSTATUSFIELD列を返すことができるようにする.csファイルでaspxページ

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" 
    Font-Bold="True" Font-Size="Small" GridLines="None"> 
    <Columns> 
     <asp:BoundField ItemStyle-Width="150px" DataField="enquiryid" HeaderText="Enquiryid" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="packagename" HeaderText="Package" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="name" HeaderText="Name" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="gender" HeaderText="Gender" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="mobileno" HeaderText="Mobile No" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="email" HeaderText="Email" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="noofdays" HeaderText="No. of Days" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="child" HeaderText="No. of Children" /> 
     <asp:BoundField ItemStyle-Width="150px" DataField="adults" HeaderText="No of Adults" /> 
     <asp:TemplateField HeaderText="Status Field"> 
      <ItemTemplate> 
       <asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="MYCOMMAND" 
        Text='<%# Bind("statusfield")%>' BorderStyle="None" Font-Bold="True" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView> 

は、あなたのBindGrid方法を変更します。

+0

私はページをリロードしてもボタンの値は変わらないが、ステータスフィールドであるテーブルの列全体が変更されて、ただ1つの行を更新する方法を教えてくれますか? @Hemant D –

+0

あなたの主キーに基づいて更新ステートメントのどこに条件を追加する必要があります。 –

+0

私は、更新クエリで条件をどこで使用する必要があるのか​​知っていますが、どのコントロールを使用する必要があるのですか? @Hemant D –

関連する問題