首先单击ASP.NET控件不会触发单击事件

时间:2017-04-18 18:45:20

标签: c# asp.net gridview

我在我的两个页面上看到了这种行为,但我只是想问一下目前对我来说更重要的一个。我有一个页面,将数据库中的信息加载到ASP网格视图中,然后允许用户向每个填充的行添加一个细节。

我遇到的问题是,当gridview的“编辑”按钮,然后是“更新”或“取消”按钮时,需要两次点击才能实际触发onclick事件。第一次点击时会发生回帖,但实际上并没有发生任何事情。

我包含下面似乎相关的代码。该页面使用母版页,并且有许多与格式有关的div,我将其排除在外。

Gridview和相关控件:

<asp:UpdatePanel runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Label Text="Plant Selector:  " runat="server" />
        <asp:DropDownList ID="ddlPlant" OnSelectedIndexChanged="ddlPlant_SelectedIndexChanged" runat="server" />
        <asp:Button ID="btnUpdate" Text="Update" OnClick="btnUpdate_Click" runat="server" />
        <p />
        <asp:Label ID="lblTest" Text="" runat="server" />
        <asp:Label ID="lblerror" Text="" ForeColor="Red" runat="server" />
        <asp:GridView ID="gridview1" AutoGenerateColumns="false" runat="server" OnRowEditing="gridview1_RowEditing" OnRowCancelingEdit="gridview1_RowCancelingEdit" OnRowUpdating="gridview1_RowUpdating">
            <Columns>
                <asp:BoundField DataField="JobNum" HeaderText="Job Number" ReadOnly="true" />
                <asp:BoundField DataField="ModelNum" HeaderText="Model" ReadOnly="true" />
                <asp:BoundField DataField="Customer" HeaderText="Customer" ReadOnly="true" />
                <asp:BoundField DataField="SchCompDate" HeaderText="Sch Comp Date" ReadOnly="true" />
                <asp:TemplateField HeaderText="Details">
                    <EditItemTemplate>
                        <asp:TextBox ID="Txt" Width="98%" runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label Text="Click Edit to add details of exception." runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="true" />
            </Columns>
       </asp:GridView>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="gridview1" />
    </Triggers>
</asp:UpdatePanel>

以下示例图片:

Sample gridview display

这是背后的代码:

private string Plant { get; set; }

// This sets the default plant based off IP.
protected void Page_PreInit(Object sender, EventArgs e)
{        
    getPlantFromIP();
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        populateDDL();
        BindData();
    }
    else
    {
        Plant = ddlPlant.SelectedValue.ToString();
    }
}

// Populates the drop down.
private void populateDDL()
{        
    ddlPlant.Items.Add("NC");
    ddlPlant.Items.Add("WA");

    setPlantInDDL();
}

private void setPlantInDDL()
{
    if(Plant == "WA")
    {
        ddlPlant.SelectedIndex = 1;
    }
    if (Plant == "NC")
    {
        ddlPlant.SelectedIndex = 0;
    }
}

private void getPlantFromIP()
{
    if (Request.ServerVariables["REMOTE_ADDR"] == "70.103.118.100")
    {
        Plant = "WA";
        //ddlPlant.SelectedIndex = 1;
    }
    else
    {
        Plant = "NC";
        //ddlPlant.SelectedIndex = 0;
    }
}

// Database Query.
private DataTable getDataFromDatabase()
{
    DataTable rTable = new DataTable();

    string plant = ddlPlant.SelectedValue.ToString();

    using (var conn = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["workorderConnectionString"].ConnectionString))
    {
        conn.Open();
        using (var cmd = conn.CreateCommand())
        {
            try
            {
                cmd.CommandText = @"SELECT * FROM reportdatatables.compliance_exception_report
                                    WHERE ExceptionNoted = '0' AND Plant = @plant";
                cmd.Parameters.AddWithValue("@plant", plant);

                MySqlDataReader reader = cmd.ExecuteReader();

                rTable.Load(reader);
                reader.Close();
                cmd.Dispose();
            }
            catch
            {

            }
            finally
            {
                conn.Close();
            }
        }
    }

    return rTable;
}

// Binds the data from the database to the gridview.
private void BindData()
{        
    DataTable data = getDataFromDatabase().Copy();

    gridview1.DataSource = data;
    gridview1.DataBind();
}


protected void ddlPlant_SelectedIndexChanged(object sender, EventArgs e)
{
    //Plant = ddlPlant.SelectedValue.ToString();
    BindData();
}

// On edit call.
protected void gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{

}

// On cancel call.
protected void gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    gridview1.EditIndex = -1;
}

protected void gridview1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

}

protected void btnUpdate_Click(object sender, EventArgs e)
{
    BindData();
}

以下是我尝试过的内容:

- 很多帖子都看到了与控件的autopostback设置有关的这种行为。正如您所看到的,我确保无法控制autopostback设置为true。

- 我担心行为可能与更新面板有关,但删除它并不会改变行为。

- 我读到你的页面标签中有AutoEventWireup =“true”会导致这种情况。我在我的页面标记中有这个,但将其设置为false并不能解决问题并阻止我的下拉列表在页面加载时填充。

- 有另一篇帖子暗示控件的ID可能在页面加载和回发之间发生变化。我监控了这些控件的ID,我发现他们的ID没有任何变化。

所有这一切,我希望有人知道我错过了什么。如果我能提供更多可能有帮助的信息,请告诉我。

提前谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个,这将使网格可编辑

protected void gridview1_RowEditing(object sender, GridViewEditEventArgs e)
{
   GridView1.EditIndex = e.NewEditIndex;
   BindData();
}

取消也是

protected void gridview1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    gridview1.EditIndex = -1;
    BindData();
}