【问题标题】:Selected a row in a grid view, now want to update that record with update button click在网格视图中选择了一行,现在想要通过单击更新按钮来更新该记录
【发布时间】:2013-08-09 19:19:24
【问题描述】:

我一直坚持这一点,并尝试了一些不同的方法来使用更新按钮来更新我从网格视图中选择的记录。我在表 ID 和名称中有 2 列。我选择了记录,它会填充一个带有名称的文本框......这很好用。在使用更新按钮单击事件将其拉入文本框后,我只需要获取相同的记录并从相同的文本框中更新名称。我还有另外两个可以正常工作的按钮,即“添加”和“删除”,我也会添加该代码,但代码如下:

这是我在页面加载或调用方法时填充网格视图的方式:

private void PopulateCompanyListGrid() //This will populate the grid with the table data on page load
    {
        IList<Company> companies;

        using (var context = new IMSDBContext())
        {
            companies = context.Companies.ToList();
        }

        grdvwCompanyList.DataSource = companies;
        grdvwCompanyList.DataBind();
    }

这是网格视图的设置方式:

<asp:GridView runat="server" ID="grdvwCompanyList" OnSelectedIndexChanged="SelectGridRow" DataKeyNames="Id, Name" AllowSorting="True" AutoGenerateSelectButton="True"></asp:GridView>

这就是我将所选记录放入文本框中的方式:

public void SelectGridRow(object sender, EventArgs e) //This will populate the textbo with the row selected from the gridview
    {
        GridViewRow name = grdvwCompanyList.SelectedRow;

        if (name != null)
        {
            var dataKey = grdvwCompanyList.DataKeys[name.RowIndex];
            if (dataKey != null)
                txtCompanyName.Text = (string)dataKey["Name"];


        }
    }

这就是我添加记录的方式:

protected void btnAdd_Click(object sender, EventArgs e) // This method adds a record to the database
    {
        if (btnAdd.Text == "Add") // Clears the textbox and notification label and calls method to change name of button if the button says "Add"
        {
            txtCompanyName.Text = "";
            lblCompanyNameNotification.Text = "";
            ButtonChangeAddToSave();
        }
        else if (btnAdd.Text == "Save") // Checks if the button says "Save" and compares textbox and database for a matching record 
        {
            IMSDBContext context = new IMSDBContext();
            Company CompanyCheck = context.Companies.SingleOrDefault(Company => Company.Name == txtCompanyName.Text);

            if (CompanyCheck != null) // Displays a notification if there is already a matching record
            {
                lblCompanyNameNotification.Text = "There is already a Company with that name.";
            }
            else if(txtCompanyName.Text == null)
            { 
                lblCompanyNameNotification.Text = "Please enter a name of a company";
            }
            else if (txtCompanyName.Text != null) // Write the record to the database if no matching record in the database
            {
                Company n = new Company();
                n.Name = txtCompanyName.Text.ToString();


                context.Companies.Add(n);
                context.SaveChanges();

                txtCompanyName.Text = "";
                lblCompanyNameNotification.Text = "";

                ButtonChangeSaveToAdd();
            }
        }
        PopulateCompanyListGrid(); // Calls method to repopulate the gridview
    }

【问题讨论】:

  • 显示你的更新方法。

标签: asp.net button gridview textbox


【解决方案1】:

在标记中添加一个隐藏字段来保存公司 ID:

<asp:HiddenField ID="hdnCompanyId" runat="server" ></asp:HiddenField>

在 selectGridRow 方法中,使用公司 ID 填充隐藏字段:

public void SelectGridRow(object sender, EventArgs e) //This will populate the textbo with the row selected from the gridview
{
    GridViewRow name = grdvwCompanyList.SelectedRow;

    if (name != null)
    {
        var dataKeys = grdvwCompanyList.DataKeys[name.RowIndex];
        if (dataKeys["Name"] != null)
            txtCompanyName.Text = (string)dataKeys["Name"];
        if (dataKeys["Id"] != null)
            hdnCompanyId.Value = dataKeys["Id"].ToString();
    }
}

在 btnUpdate_Click 方法中,通过 Id 获取公司并更新它:

protected void btnUpdate_Click(object sender, EventArgs e) 
{
    int companyId;
    string companyName = txtCompanyName.Text;
    if(int.TryParse(hdnCompanyId.Value, out companyId)){

        IMSDBContext context = new IMSDBContext();
        Company company = context.Companies.SingleOrDefault(Company => Company.Id == companyId);

        if (company != null && txtCompanyName.Text != "")
        {
            company.Name = companyName;
            context.SaveChanges();

        }
        else
        {
            lblCompanyNameNotification.Text = "The  Company does not exist.";
        }
    }

    PopulateCompanyListGrid(); // Calls method to repopulate the gridview            
}

【讨论】:

  • 我在周末回家之前发布了这个星期五的帖子,在如何将名称绑定到网格视图中所做的选择时困惑了一分钟。在今天早上上班的路上,我突然意识到我需要完全按照你发布的内容去做。我脑子里有我需要做的事情,我进来第一件事就是看到你的帖子。至少我知道我今天早上进来的时候是朝着正确的方向前进的。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 2019-11-15
  • 2019-04-25
  • 1970-01-01
  • 2013-03-14
相关资源
最近更新 更多