2016-09-13 11 views
1

SQLデータベースにCountryという名前のテーブルがあります。これでaspでウェブサイトが作成されました。グリッドビュー用に設計されています。 sqlのthrougnグリッドビュー、私は3つのテキストボックス "CountryID" "名前" "CountryNotes"とWebページのADDボタン、私はデータベースに保存され、同じページ自体を更新パネルを使用して、 gridviewのテーブルを編集したいので、gridviewの各行でeditボタンをクリックすると、選択された行の値がページ上部にあるtexboxesに渡されます。これは、テキストボックスがgridviewの外側にあることを意味します。そして私はこれは私のC#でグリッドビューの行からデータを取得し、グリッドビュー外のテキストボックスに渡します

<form id="form1" runat="server"> 
    <asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager> 
<div> 

    <table align="center" style="width:50%;"> 
     <tr> 
      <td class="auto-style6"> 
       <asp:Label ID="Label3" runat="server" Text="Country ID" Font-Bold="True" ForeColor="Red"></asp:Label> 
      </td> 
      <td class="auto-style7"> 
       <asp:TextBox ID="Text0" runat="server" Width="138px" ></asp:TextBox> 
      </td> 
      <td class="auto-style8"></td> 
      <td class="auto-style9"> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="auto-style1"> 
       <asp:Label ID="Label1" runat="server" Text="Country Name" Font-Bold="True" ForeColor="Red"></asp:Label> 
      </td> 
      <td class="auto-style4"> 
       <asp:TextBox ID="Text1" runat="server" Width="137px"></asp:TextBox> 
      </td> 
      <td>&nbsp;</td> 
      <td class="auto-style5"> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td class="auto-style1"> 
       <asp:Label ID="Label2" runat="server" Text="Country Notes" Font-Bold="True" ForeColor="Red"></asp:Label> 
      </td> 
      <td class="auto-style4"> 
       <asp:TextBox ID="Text2" runat="server" Width="136px"></asp:TextBox> 
      </td> 
      <td>&nbsp;</td> 
      <td class="auto-style5"> 
       &nbsp;</td> 
     </tr> 
     <tr> 
      <td> 
       <br /> 
       <asp:Button ID="Button1" runat="server" Text="Add" BackColor="#990000" ForeColor="White" OnClick="Button1_Click" /> 
      </td> 
      <td > 
       <br /> 
       </td> 
      <td>&nbsp;</td> 
      <td class="auto-style5">&nbsp;</td> 
     </tr> 

    </table> 

    <br /> 
    <br /> 
<asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <Triggers> 

       <asp:AsyncPostBackTrigger ControlID="GridView1" EventName="PageIndexChanging" /> 

      </Triggers> 
    <ContentTemplate> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataKeyNames="CountryID" DataSourceID="SqlDataSource1" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"> 
     <Columns> 







      <asp:BoundField DataField="CountryID" HeaderText="CountryID" InsertVisible="False" ReadOnly="True" SortExpression="CountryID" /> 
      <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> 
      <asp:BoundField DataField="CountryNotes" HeaderText="CountryNotes" SortExpression="CountryNotes" /> 
      <asp:ButtonField ButtonType="Button" CommandName="EditRow" /> 

       <asp:CommandField ShowDeleteButton="True" ShowEditButton="true"/> 


     </Columns> 
     <FooterStyle BackColor="White" ForeColor="#000066" /> 
     <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> 
     <RowStyle ForeColor="#000066" /> 
     <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> 
     <SortedAscendingCellStyle BackColor="#F1F1F1" /> 
     <SortedAscendingHeaderStyle BackColor="#007DBB" /> 
     <SortedDescendingCellStyle BackColor="#CAC9C9" /> 
     <SortedDescendingHeaderStyle BackColor="#00547E" /> 
    </asp:GridView> 
     </ContentTemplate> 
</asp:UpdatePanel> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ATSConnectionString %>" DeleteCommand="DELETE FROM [Country1] WHERE [CountryID] = @original_CountryID AND [Name] = @original_Name AND [CountryNotes] = @original_CountryNotes" InsertCommand="INSERT INTO [Country1] ([CountryID], [Name], [CountryNotes]) VALUES (@CountryID, @Name, @CountryNotes)" SelectCommand="SELECT * FROM [Country1]" UpdateCommand="UPDATE [Country1] SET [Name] = @Name, [CountryNotes] = @CountryNotes WHERE [CountryID] = @original_CountryID AND [Name] = @original_Name AND [CountryNotes] = @original_CountryNotes" ConflictDetection="CompareAllValues" OldValuesParameterFormatString="original_{0}"> 
     <DeleteParameters> 
      <asp:Parameter Name="original_CountryID" Type="Int32" /> 
      <asp:Parameter Name="original_Name" Type="String" /> 
      <asp:Parameter Name="original_CountryNotes" Type="String" /> 
     </DeleteParameters> 
     <InsertParameters> 
      <asp:Parameter Name="CountryID" Type="Int32" /> 
      <asp:Parameter Name="Name" Type="String" /> 
      <asp:Parameter Name="CountryNotes" Type="String" /> 
     </InsertParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="Name" Type="String" /> 
      <asp:Parameter Name="CountryNotes" Type="String" /> 
      <asp:Parameter Name="original_CountryID" Type="Int32" /> 
      <asp:Parameter Name="original_Name" Type="String" /> 
      <asp:Parameter Name="original_CountryNotes" Type="String" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 

</div> 
</form> 

これは私のHTMLページでコンテンツを編集し、ボタンを追加する意味クリック更新ボタンでデータを更新する必要があり、ボタン

を更新するために変更する必要があります

public partial class atc : System.Web.UI.Page 
{ 

    SqlConnection con; 
    SqlCommand cmd; 
    public void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      GridView1.DataBind(); 
     } 

    } 


    protected void Button1_Click(object sender, EventArgs e) 
    { 
     con = new SqlConnection("Data Source=RYI-SYS-004;Initial Catalog=ATS;Integrated Security=True"); 
     cmd = new SqlCommand("insert into Country1 (CountryID,Name,CountryNotes) values(@CountryID,@Name, @CountryNotes)", con); 

     cmd.Parameters.AddWithValue("@CountryID", Text0.Text); 
     cmd.Parameters.AddWithValue("@Name", Text1.Text); 
     cmd.Parameters.AddWithValue("@CountryNotes", Text2.Text); 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 

     if (IsPostBack) 
     { 
      Text0.Text = ""; 
      Text1.Text = ""; 
      Text2.Text = ""; 
      GridView1.DataBind(); 
     } 

    } 

} 

コーディングこれが何であるか、私の既存の出力

enter image description here

誰でもこの問題を解決できますか?

+0

Text0.Text = GridView1.Rows [ROW_NUM] [COL_NUM]、Text; 2行目と0列目は0であり、2行目は1であることに注意してください。 –

+0

私は申し訳ありませんが、あなたがコメントしたコーディングをどこに挿入すればいいのか分かりません。だから知っていれば、それを正しく指定できる@Shannon Holsinger –

答えて

1
Note - there are a few changes you;ll want to make to this code. The example I'm giving you is not meant to be optimized or generalized - it's designed to answer your specific question while changing as little of your original code as possible. There may be other errors. 

public partial class atc : System.Web.UI.Page 
{ 

SqlConnection con; 
SqlCommand cmd; 
public void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     GridView1.DataBind(); 
    } 

} 


protected void Button1_Click(object sender, EventArgs e) 
{ 
    con = new SqlConnection("Data Source=RYI-SYS-004;Initial Catalog=ATS;Integrated Security=True"); 
    cmd = new SqlCommand("insert into Country1 (CountryID,Name,CountryNotes) values(@CountryID,@Name, @CountryNotes)", con); 

    cmd.Parameters.AddWithValue("@CountryID", Text0.Text); 
    cmd.Parameters.AddWithValue("@Name", Text1.Text); 
    cmd.Parameters.AddWithValue("@CountryNotes", Text2.Text); 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    con.Close(); 

    if (IsPostBack) 
    { 
     Text0.Text = GridView1[ROW_NUM][COL_NUM].Text; 
     Text1.Text = GridView1[ROW_NUM][COL_NUM].Text; 
     Text2.Text = GridView1[ROW_NUM][COL_NUM].Text; 
     // GridView1.DataBind(); 
    } 

} 

} 

あなたは[4] searchRowがintである。ここで、[0] GridView1のように、この仕事をするために上記の各ROW_NUMとCOL_NUMのための適切な値を入力する必要が[0]またはGridview1 [searchRow]。

関連する問題