1

VisualStudio2012およびSQLServer2012の使用

状況は次のとおりです。

私が構築しているWebサイトは、SQLDataSourceからフィールドにロードされたデータを更新していません。データは適切なテキストボックスに正常に読み込まれますが、データを編集してボタンクリックをアクティブにすると、リビジョンがクリアされ、ページが元の読み込まれたデータに更新されたように見えます。私の知る限り、私のコードは正しいように見えますが、明らかにそうではありません。

追加情報:

マスターページを含むcontainerplaceholderにページを設定しました。

これが私のマークアップです:

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<!-- Job Title -->

<div class="lbljobtitle">
    <asp:Label ID="lblJobTitle" runat="server" Text="Job Title"></asp:Label>
</div>

<div class="txtjobtitle">
    <asp:TextBox ID="txtJobTitle" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>

<!-- Employee Type -->

<div class="lblemployeetype">
    <asp:Label ID="lblEmployeeType" runat="server" Text="Employee Type"></asp:Label>
</div>

<div class="txtemployeetype">
    <asp:TextBox ID="txtEmployeeType" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>

<!-- Location -->

<div class="lbllocation">
    <asp:Label ID="lblLocation" runat="server" Text="Location"></asp:Label>
</div>

<div class="txtlocation">
    <asp:TextBox ID="txtLocation" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>

<!-- Job Description -->

<div class="lbljobdescription">
    <asp:Label ID="lblJobDescription" runat="server" Text="Job Description"></asp:Label>
</div>

<div class="txtjobdescription">
    <asp:TextBox ID="txtJobDescription" runat="server" Height="160px" Width="250px" TextMode="MultiLine"></asp:TextBox>
</div>

<!-- Requirements -->

<div class="lblrequirements">
    <asp:Label ID="lblRequirements" runat="server" Text="Requirements"></asp:Label>
</div>

<div class="txtrequirements">
    <asp:TextBox ID="txtRequirements" runat="server" Height="160px" Width="250px" TextMode="MultiLine"></asp:TextBox>
</div>

<!-- Experience -->

<div class="lblexperience">
    <asp:Label ID="lblExperience" runat="server" Text="Experience"></asp:Label>
</div>

<div class="txtexperience">
    <asp:TextBox ID="txtExperience" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>

<!-- Date Posted -->

<div class="lbldateposted">
    <asp:Label ID="lblDatePosted" runat="server" Text="Date Posted"></asp:Label>
</div>

<div class="txtdateposted">
    <asp:TextBox ID="txtDatePosted" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>

<!-- Start Date -->

<div class="lblstartdate">
    <asp:Label ID="lblStartDate" runat="server" Text="Start Date"></asp:Label>
</div>

<div class="txtstartdate">
    <asp:TextBox ID="txtStartDate" runat="server" Height="22px" Width="250px"></asp:TextBox>
</div>


<asp:Button ID="btnUpdate" runat="server" Text="Update Job" OnClick="btnUpdate_Click" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:bgrecruit_d01ConnectionString %>"
    SelectCommand="SELECT job_title, employee_type, location, job_description, requirements, experience, date_posted, start_date, requisition_id FROM jobopening_tbl WHERE (requisition_id = @requisition_id)"
    UpdateCommand="UPDATE [jobopening_tbl] SET [job_title] = @job_title, [employee_type] = @employee_type, [location] = @location, [job_description] = @job_description, [requirements] = @requirements, [experience] = @experience, [date_posted] = @date_posted, [start_date] = @start_date WHERE [requisition_id] = @requisition_id">

    <SelectParameters>
        <asp:QueryStringParameter Name="requisition_id" Type="String" QueryStringField="req_id" />
    </SelectParameters>

    <UpdateParameters>

        <asp:Parameter Name="job_title" Type="String" />
        <asp:Parameter Name="employee_type" Type="String" />
        <asp:Parameter Name="location" Type="String" />
        <asp:Parameter Name="job_description" Type="String" />
        <asp:Parameter Name="requirements" Type="String" />
        <asp:Parameter Name="experience" Type="String" />
        <asp:Parameter Name="date_posted" Type="String" />
        <asp:Parameter Name="start_date" Type="String" />
        <asp:Parameter Name="requisition_id" />
    </UpdateParameters>


</asp:SqlDataSource>

そして私のC#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class UpdateDetail : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    DataRowView row = dv[0];

    txtJobTitle.Text = row["job_title"].ToString();
    txtEmployeeType.Text = row["employee_type"].ToString();
    txtLocation.Text = row["location"].ToString();
    txtJobDescription.Text = row["job_description"].ToString();
    txtRequirements.Text = row["requirements"].ToString();
    txtExperience.Text = row["experience"].ToString();
    txtDatePosted.Text = row["date_posted"].ToString();
    txtStartDate.Text = row["start_date"].ToString();             

}
protected void btnUpdate_Click(object sender, EventArgs e)
{
    DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
    DataRowView row = dv[0];

    SqlDataSource1.UpdateParameters["job_title"].DefaultValue = txtJobTitle.Text.ToString();
    SqlDataSource1.UpdateParameters["employee_type"].DefaultValue = txtEmployeeType.Text.ToString();
    SqlDataSource1.UpdateParameters["location"].DefaultValue = txtLocation.Text.ToString();
    SqlDataSource1.UpdateParameters["job_description"].DefaultValue = txtJobDescription.Text.ToString();
    SqlDataSource1.UpdateParameters["requirements"].DefaultValue = txtRequirements.Text.ToString();
    SqlDataSource1.UpdateParameters["experience"].DefaultValue = txtExperience.Text;
    SqlDataSource1.UpdateParameters["date_posted"].DefaultValue = txtDatePosted.Text;
    SqlDataSource1.UpdateParameters["start_date"].DefaultValue = txtStartDate.Text;
    //requisition_id = @requisition_id
    SqlDataSource1.UpdateParameters["requisition_id"].DefaultValue = row["requisition_id"].ToString();

    SqlDataSource1.Update();

}

}

4

1 に答える 1

0

次のブロックを追加してif、page_loadのコードが次のようになるようにします。

if(!Page.IsPostBack)
{
  DataView dv = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
  DataRowView row = dv[0];

  txtJobTitle.Text = row["job_title"].ToString();
  txtEmployeeType.Text = row["employee_type"].ToString();
  txtLocation.Text = row["location"].ToString();
  txtJobDescription.Text = row["job_description"].ToString();
  txtRequirements.Text = row["requirements"].ToString();
  txtExperience.Text = row["experience"].ToString();
  txtDatePosted.Text = row["date_posted"].ToString();
  txtStartDate.Text = row["start_date"].ToString();  
}

問題は、page_loadがすべてのポストバック(ボタンのクリックを含む)で実行されることです。そのコードを実行したいのは、ページが最初に読み込まれたときだけだと思います。これは、追加がif行うことです。

.NETでプログラミングを続けると、この問題が約28972回発生するため、これを覚えておいてください。

于 2012-12-13T21:25:02.303 に答える