如何使用表单视图编辑某些数据,某些数据仍然无法编辑?

时间:2015-06-30 03:59:31

标签: c# asp.net formview

我在aspx.cs页面中有这个代码。在我的数据库中,我有列UsernameFirstnameLastnameEmailPasswordCustomerTypeDeliveryAddress,{ {1}}和Zipcode

我想要做的是用户名和客户类型仍然是不可编辑的,其他人可以由用户编辑。

aspx.cs

Contact number

2 个答案:

答案 0 :(得分:0)

对要编辑的列使用EditItemTemplate。例如:

<EditItemTemplate>
                  <table>


                    <tr><td align="right"><b>First Name:</b></td>
                        <td><asp:TextBox ID="txtFirstName2"
                                         Text='<%# Bind("FirstName") %>'
                                         RunAt="Server" /></td></tr>

                    <tr><td align="right"><b>Last Name:</b></td>
                        <td><asp:TextBox ID="txtLastName2"
                                         Text='<%# Bind("LastName") %>'
                                         RunAt="Server" /></td></tr>                                 
                    <tr>
                      <td colspan="2">
                        <asp:LinkButton ID="UpdateButton"
                                        Text="Update"
                                        CommandName="Update"
                                        RunAt="server"/>
                          &nbsp;
                        <asp:LinkButton ID="CancelUpdateButton"
                                        Text="Cancel"
                                        CommandName="Cancel"
                                        RunAt="server"/>
                      </td>
                    </tr>
                  </table>                
</EditItemTemplate>

答案 1 :(得分:0)

我设法解决了这个问题。感谢@Litisqe Kumar给我一个关于问题的好主意。

鉴于上面的答案,您应该将可编辑字段放在<EditTemplate>内,这是后面的代码。

    string connStr = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
    SqlDataAdapter sqlda = new SqlDataAdapter();
    SqlCommand com = new SqlCommand();
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["New"] != null)
        {
            if (!IsPostBack)
            {
                bindgrid();
            }
        }
    }
    private void bindgrid()
    {
        SqlConnection conn = new SqlConnection(connStr);
        dt = new DataTable();
        com.Connection = conn;
        com.CommandText = "SELECT * FROM UserData WHERE Username ='" + Session["New"] + "'";
        sqlda = new SqlDataAdapter(com);
        sqlda.Fill(dt);
        EmployeeFormView.DataSource = dt;
        EmployeeFormView.DataBind();

    }

    protected void EmployeeFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {

        DataKey key = EmployeeFormView.DataKey;
        TextBox txtFirstName = (TextBox)EmployeeFormView.FindControl("txtFirstName2");
        TextBox txtLastName = (TextBox)EmployeeFormView.FindControl("txtLastName2");
        TextBox txtPass = (TextBox)EmployeeFormView.FindControl("txtPassword2");
        TextBox txtAddress = (TextBox)EmployeeFormView.FindControl("txtAddress2");
        TextBox txtZip = (TextBox)EmployeeFormView.FindControl("txtZip2");
        TextBox txtContact = (TextBox)EmployeeFormView.FindControl("txtContact2");

        SqlConnection conn = new SqlConnection(connStr);
        com.Connection = conn;
        com.CommandText = "UPDATE UserData SET FirstName ='" + txtFirstName.Text + "',LastName ='" + txtLastName.Text + "',DeliveryAddress ='" + txtAddress.Text + "', Zip ='"+txtZip.Text+ "',Password ='"+ txtPass.Text+"',ContactNumber ='" + txtContact.Text + "'   WHERE ID ='" + key.Value.ToString() + "'";
        conn.Open();
        com.ExecuteNonQuery();
        Response.Write("Record updated successfully");
        bindgrid();
        conn.Close();
    }
    protected void EmployeeFormView_ModeChanging(object sender, FormViewModeEventArgs e)
    {
        EmployeeFormView.ChangeMode(e.NewMode);
        bindgrid();
        if (e.NewMode == FormViewMode.Edit)
        {
            EmployeeFormView.AllowPaging = false;
        }
        else
        {
            EmployeeFormView.AllowPaging = true;
        }
    }

    protected void EmployeeFormView_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        EmployeeFormView.ChangeMode(FormViewMode.ReadOnly);
    }