通过SQLDataSource更新文本框中的数据

时间:2012-12-13 21:21:47

标签: asp.net sql-server

使用Visual Studio 2012和SQL Server 2012

情况如下:

我正在构建的网站不会更新从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();

}

}

1 个答案:

答案 0 :(得分: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次。