iT邦幫忙

0

ASP Webform 的click事件會跳出警告框,需在按確認時呼叫server side的函式,但按完後還是會跳出警告框

  • 分享至 

  • xImage

目前有一個GridView/更新資產資訊的btn跟後端的程式如下,
情境:
當按下更新資產,for loop DataGrid的資產,取得方式選(庫存),會select 倉庫的數量,如果數量小於0就會觸發confirm的警告框,通知user是否還要預訂,按下確認鈕就會呼叫server side的函式;否就Cancel
問題:
按下確認後還是會一直跑出警告框,按下否才會不出現警告框,目前有嘗試用session或viewstate的方式去處理,但後續按下更新資產資訊就不會出現警告框。

前端

<asp:DataGrid ID="dagRequestItem_H" runat="server" DataKeyField="id" CellPadding="0"
AutoGenerateColumns="False" ShowHeader="true" GridLines="None" Width="100%" 
OnDeleteCommand="dagRequestItem_H_DeleteCommand" OnRowDataBound="myGridView_RowDataBound" >
<Columns>
    <asp:EditCommandColumn ButtonType="LinkButton" Visible="false"></asp:EditCommandColumn>
    <asp:BoundColumn Visible="False" DataField="ID" ReadOnly="True" HeaderText="id">
    </asp:BoundColumn>
      <asp:TemplateColumn HeaderText="Method of obtaining/取得方式" >
        <ItemTemplate>
            <asp:DropDownList ID="ddlAcquirement"  runat="server"    >
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Category/資產" >
        <ItemTemplate>
            <asp:Label ID="lblCategory"  runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.category") %>'  >
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateColumn>
      <asp:TemplateColumn HeaderText="Brand/品牌" >
        <ItemTemplate>
            <asp:DropDownList ID="ddlBrand"  runat="server"   AutoPostBack="True" OnSelectedIndexChanged="ddlBrand_SelectedIndexChanged">
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Model/機型" >
        <ItemTemplate>
            <asp:DropDownList ID="ddlModel"  runat="server" AutoPostBack="True">
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Using Type/使用類型">
    <ItemTemplate>
        <asp:Label ID="lblUsingType"  runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.using_type") %>'>
        </asp:Label>
    </ItemTemplate>
</asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Qty/數量" >
        <ItemTemplate>
            <asp:Label ID="lblQuantity"  runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.qty") %>'>
            </asp:Label>
        </ItemTemplate>
    </asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:Button ID="btnHDCheckAsset" class="btn btn-info" runat="server" Text="更新資產資訊" OnClick="btnHDCheckAsset_Click" />

後端click跟page_load,目前僅先撰寫測試,還沒根據上面的情境去寫,想要先成功才去改

     protected void btnHDCheckAsset_Click(object sender, EventArgs e)
        {        
            bool flag = true;
            foreach (DataGridItem item in dagRequestItem_H.Items)
            {
                if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                {
                    DropDownList ddlAcquirement = (DropDownList)item.FindControl("ddlAcquirement");
                    string acquirement = ddlAcquirement.SelectedValue;
                    if (acquirement == "")
                    {
                        //取得方式為空就把資產得文字改為test1
                        flag = false;
                        System.Web.UI.WebControls.Label lblCategory = (System.Web.UI.WebControls.Label)item.FindControl("lblCategory");
                        lblCategory.Text = "test1";
                    }
                }
            }
            if (flag)
            {             
                string script = "if (confirm('Do you want to continue?')) { __doPostBack('" + btnHDCheckAsset.UniqueID + "', ''); }";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "confirm", script, true);
            }
        }
        
        
   public override void afterInitPage(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                setForm_DefaulValue();
              
            }
            else
            {//按下確認時會呼叫得postback事件
                string controlId = Request.Params.Get("__EVENTTARGET");
                if (controlId == "btnHDCheckAsset")
                {
                    foreach (DataGridItem item in dagRequestItem_H.Items)
                    {
                        if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
                        {                          
                            System.Web.UI.WebControls.Label lblCategory = (System.Web.UI.WebControls.Label)item.FindControl("lblCategory");
                           lblCategory.Text="test";                    
                        }
                    }
                }
            }
        }       
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
Nash
iT邦新手 4 級 ‧ 2024-02-23 10:19:00

你看看這樣行不行
'''
protected void btnHDCheckAsset_Click(object sender, EventArgs e)
{
bool flag = true;
foreach (DataGridItem item in dagRequestItem_H.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
DropDownList ddlAcquirement = (DropDownList)item.FindControl("ddlAcquirement");
string acquirement = ddlAcquirement.SelectedValue;
if (acquirement == "")
{
flag = false;
System.Web.UI.WebControls.Label lblCategory = (System.Web.UI.WebControls.Label)item.FindControl("lblCategory");
lblCategory.Text = "test1";
}
}
}
if (flag)
{

    btnHDCheckAsset.Enabled = false;
    

    string script = "if (confirm('Do you want to continue?')) { " +
                    "__doPostBack('" + btnHDCheckAsset.UniqueID + "', ''); }";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "confirm", script, true);
}

}
'''

我要發表回答

立即登入回答