Domanda

I am using a GridView to display error information to be verified by the user. Then the user is given fillable fields (datepicker, dropdownlist, textbox) to fix the errors. These are outside of the GridView.

After, they have a submit button to save the changes to a sql log. The problem I am having is that because I'm passing in object sender, GridViewRowEventsArg e, the button doesn't recognize the GridView and I get an error:

No overload for 'btnSaveMemberErrorEdit_ButtonClicked' matches delegate 'System.EventHandler'

I am using asp.net with c# code behind. Sample code is:

ASP:

    <div id="actionItemAccordion">
<h3>Action Items</h3>
<asp:Panel runat="server">
    <asp:GridView runat="server" ID="gridActionItems" DataKeyNames="ErrorKey"
        AutoGenerateColumns="false" EmptyDataText="No Action Items Were Found." ShowHeader="true"
        ClientIDMode="Static" Width="100%">
        <Columns>
            <asp:BoundField HeaderText="Action Item" DataField="ActionItem" />
            <asp:BoundField HeaderText="Month" DataField="Month" />
            <asp:BoundField HeaderText="Data" DataField="DCode" />
            <asp:BoundField HeaderText="Data2" DataField="D2Code" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:RadioButtonList runat="server" ID="rbWinner" AutoPostBack="true" Font-Size="Smaller" >
                        <asp:ListItem Text="D1 Correct" Enabled="true" Value="D1" />
                        <asp:ListItem Text="D2 Correct" Enabled="true" Value="D2" />
                    </asp:RadioButtonList>
                </ItemTemplate>
            </asp:TemplateField>
          </Columns>
    </asp:GridView>
    <asp:Label runat="server" BorderWidth="2px" Width="850px"></asp:Label>
    <div>
        <asp:Label runat="server" Text="Edit Any Necessary Changes" Style="text-align: center"
            Font-Size="Medium" Visible="true" Font-Bold="true" Width="100%" />
        <br />
        <br />
    </div>
    <div>
        <asp:Label runat="server" Text="Effective Date: " Style="text-align: right"
            Font-Size="Small" Visible="true" Width="200px" />
        <asp:TextBox runat="server" ID="txtEffectiveDate" Text="" Style="text-align: left"
            ClientIDMode="Static" Font-Size="13px" Columns="15" Height="15" Width="15%" />
        <asp:Label runat="server" Text="Termination Date: " Style="text-align: right"
            Font-Size="Small" Visible="true" Width="200px" />
        <asp:TextBox runat="server" ID="txtTerminationDate" Text="" Style="text-align: left"
            ClientIDMode="Static" Font-Size="13px" Columns="15" Height="15" Width="15%" />
        <br />
        <br />
    </div>
    <div>
        <asp:Label runat="server" Text="New/Corrected Name: " Style="text-align: right"
            Font-Size="Small" Visible="true" Width="200px" />
        <asp:DropDownList runat="server" ID="ddlNewCode" AutoPostBack="false" AppendDataBoundItems="true"
            Style="text-align: left" Width="220" BackColor="AliceBlue" ClientIDMode="Static"
            DataTextField="Code" DataValueField="CodeKey">
        </asp:DropDownList>
        <br />
        <br />
        <asp:Label runat="server" BorderWidth="1px" Width="850px"></asp:Label>
        <br />
    </div>
    <div>
        <asp:Label runat="server" Text="Notes: " Style="text-align: right; vertical-align: middle"
            Font-Size="Small" Visible="true" Width="200px" />
        <asp:TextBox runat="server" ID="txtNotes" Text="" Style="text-align: left" ClientIDMode="Static"
            Wrap="true" TextMode="MultiLine" Font-Size="10px" Columns="100" Height="100"
            Width="50%" />
    </div>
    <div>
        <asp:Button runat="server" ID="btnSaveErrorEdit" Text="Save Changes" OnClick="btnSaveErrorEdit_ButtonClicked"
            Height="30px" Enabled="true" Width="95px" Style="margin-left: 330px; margin-top: 70px;
            margin-bottom: 25px; padding: 0px; border-radius: 5px;" />
        <asp:Button runat="server" ID="btnCancelEdit" Text="Cancel" OnClientClick="return CancelEdit();"
            Enabled="true" Height="30px" Width="70px" Style="margin-right: 330px; margin-top: 70px;
            margin-bottom: 25px; padding: 0px; border-radius: 5px;" />
    </div>
</asp:Panel>

Code Behind:

    protected void btnSaveErrorEdit_ButtonClicked(object sender, GridViewRowEventArgs e)
    {
        int hiddenErrorKey = Convert.ToInt32(e.Row.Cells[5].Text);

        this.assignCorrect = e.Row.Cells[4].Text;           

        SqlCommand command = new SqlCommand("some_storedProc");
        command.CommandType = CommandType.StoredProcedure;

        command.Parameters.Add("@ErrorKey", SqlDbType.Int).Value = hiddenErrorKey;
        command.Parameters.Add("@EffectiveDate", SqlDbType.DateTime).Value = this.txtEffectiveDate;
        command.Parameters.Add("@TerminationDate", SqlDbType.DateTime).Value = this.txtTerminationDate;
        command.Parameters.Add("@Notes", SqlDbType.VarChar, -1).Value = this.txtNotes;
        command.Parameters.Add("@NameOfInputUser", SqlDbType.VarChar, 50).Value = this.lblInputUserName;
        command.Parameters.Add("@Code", SqlDbType.VarChar, 1).Value = this.assignWinner;
        Utilities.Exec_Qry(this.Application["some_connection"].ToString(), command);   
    }
È stato utile?

Soluzione

You need to change the function to take arguments for the asp:button

(object sender, EventArgs e) 

Then you can grab the GridView in the behind code using

var button = sender as Button;
var view = button.FindControl("gridActionItems") as GridView;

Then you can access the elements as needed.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top