收录日期:2020/12/03 00:28:04 时间:2010-09-07 20:25:33 标签:c#,asp.net

I have a FormView that I user for updating a record. There is a link button that when fires should perforom the updating via BLL and DAL. I am not using built-in ODS and I will not condsider using it.

I have all my grids and formviews populated manuualy by calling methods that fetch the data from the database.

For instance my details view is populated like this:

protected void DlMembers_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName.ToString() == "Select")
    {
        DlMembers.Visible = false;
        lblError.Text = string.Empty;
        lblError.Visible = false;
        fvMemberDetail.Visible = true;
        fvMemberDetail.ChangeMode(FormViewMode.Edit);

        MemberBLL getMemberInfo = new MemberBLL();

        int Ident = Convert.ToInt32(e.CommandArgument.ToString());

        fvMemberDetail.DataSource = getMemberInfo.GetMemberByIdent(Ident);
        fvMemberDetail.DataBind();

    }

    if (e.CommandName.ToString() == "DeleteSelected")
    {
        DlMembers.Visible = true;
        lblError.Text = string.Empty;
        lblError.Visible = false;
        fvMemberDetail.Visible = false;
        fvMemberDetail.ChangeMode(FormViewMode.ReadOnly);
  }

What I want to do if to capature my linkbutton on click event and do this (except that the runtime never reaches this method):

protected void MemberInfoUpdating(object sender, EventArgs e)
{
    TextBox id = (TextBox)fvMemberDetail.FindControl("txtIdent");

    if (id.Text != string.Empty || id.Text != "")
    {
        TextBox txtFN = (TextBox)fvMemberDetail.FindControl("txtFN");
        TextBox txtLN = (TextBox)fvMemberDetail.FindControl("txtLN");
        DropDownList ddlAddress = (DropDownList)fvMemberDetail.FindControl("ddlAddress");
        TextBox txtEmail = (TextBox)fvMemberDetail.FindControl("txtEmail");
        TextBox txtHPhone = (TextBox)fvMemberDetail.FindControl("txtHPhone");
        TextBox txtWPhone = (TextBox)fvMemberDetail.FindControl("txtWPhone");
        TextBox txtMPhone = (TextBox)fvMemberDetail.FindControl("txtMPhone");
        DropDownList ddlPos = (DropDownList)fvMemberDetail.FindControl("ddlPos");
        DropDownList ddlIsAdmin = (DropDownList)fvMemberDetail.FindControl("ddlIsAdmin");
        bool blIsAdmin = false;
        if (ddlIsAdmin.SelectedValue == "True") blIsAdmin = true;
        TextBox txtComments = (TextBox)fvMemberDetail.FindControl("txtComments");

        MemberBLL updateMemberInfo = new MemberBLL();

        bool UpdateOK = updateMemberInfo.UpdateMemberByIdent(
                        txtFN.Text,
                        txtLN.Text,
                        ddlAddress.SelectedValue,
                        txtEmail.Text,
                        txtHPhone.Text,
                        txtWPhone.Text,
                        txtMPhone.Text,
                        blIsAdmin,
                        txtComments.Text,
                        Convert.ToInt32(ddlPos.SelectedValue),
                        Convert.ToInt32(id.Text));
    }
    else
    {
        //Display error - no user id cannot update record
    }
}

The linkbutton looks like this:

<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                OnClick="MemberInfoUpdating" Text="Update" />

Where is this LinkButton? If it's inside a FormView template, then you'll likely need to use something like this instead:

<asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />

Then handle the "Update" command in DlMembers_ItemCommand.

Alternatively, attach your code to the OnItemUpdating event of the FormView rather than some extra event you don't need:

<asp:FormView ID="fvMemberDetail" runat="server" OnItemUpdating="MemberInfoUpdating">