iT邦幫忙

0

(已解決)[asp.net,winForm,command]請教進入update模式後要怎麼回到檢視畫面

舜~ 2020-06-08 21:53:032753 瀏覽
  • 分享至 

  • xImage

我有一個ListView,使用內建的編輯、刪除,但編輯進入後更新按鈕沒有任何反應,aspx元件坑爹的是就算單步執行也不會有錯誤訊息產生...
(有人知道怎麼找出錯誤訊息的話的話請教我...)

所以乾脆直接在ListView_ItemCommand中自己來~~
更新是成功了,但遇到另一個問題,
就是我不知道怎麼回到檢視畫面

想請問各位前輩們該怎麼處理?

https://ithelp.ithome.com.tw/upload/images/20200608/20110337S3GxI6Ku0N.png

下方式部分code
aspx

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <div class="card">
            <div class="card-header">
                <strong class="card-title">試區管理 </strong>
            </div>
            <div class="card-body">
                <div class="row">
                    <div class="col-lg-3 col-md-3 col-sm-4 col-xs-6 rowCell mt-2">
                        <asp:TextBox ID="EASubject" runat="server" CssClass="form-control" placeholder="考場名稱"></asp:TextBox>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-4 col-xs-6 rowCell mt-2">
                        <asp:TextBox ID="EAAddress" runat="server" CssClass="form-control" placeholder="地址"></asp:TextBox>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-4 col-xs-6 rowCell mt-2">
                        <div class="input-group">
                            <input id="h_EADepID" name="h_EADepID" runat="server" type="hidden" />
                            <input name="EADepName" type="text" id="EADepName" placeholder="所屬單位" runat="server"  class="form-control" />
                            <div class="input-group-btn">
                                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="../Account/DepList.aspx?prefix=ctl00_ContentPlaceHolder1_&name=EADepName&value=h_EADepID" CssClass="btnColorBox"><i class="btn btn-info fa fa-search"></i></asp:HyperLink>
                            </div>
                        </div>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-4 col-xs-6 rowCell mt-2">
                        <asp:TextBox ID="EAReservedSeats" runat="server" CssClass="form-control" placeholder="單位保障名額" TextMode="Number" Min="0"  pattern="\d*" ></asp:TextBox>
                    </div>
                    <div class="col-lg-3 col-md-3 col-sm-4 col-xs-6 rowCell mt-2">
                        學校:<asp:DropDownList runat="server" ID="EAIsSchool" CssClass="btn btn-info" >
                            <asp:ListItem Value="">未選擇</asp:ListItem>
                            <asp:ListItem Value="Y">是學校</asp:ListItem>
                            <asp:ListItem Value="N">非學校</asp:ListItem>
                        </asp:DropDownList>
                    </div>
                </div>
                 <div class="row">
                    <div class="col-lg-3 col-md-3 col-sm-4 col-xs-6 rowCell mt-2">
                        <asp:Button ID="btEASerach" runat="server" Text="查詢" CssClass="btn btn-info" OnClick="btEASerach_Click" />
                    </div>
                </div>
            </div>
        </div>
        <asp:ListView ID="ListView_ExamArea" runat="server" DataKeyNames="EANBR" DataSourceID="SqlDataSource_ExamArea" GroupItemCount="3" 
            OnItemCommand="ListView_ExamArea_ItemCommand" OnPreRender="ListView_ExamArea_PreRender" OnItemDataBound="ListView_ExamArea_ItemDataBound">
            <AlternatingItemTemplate>
                <td runat="server" style="background-color: #FFF8DC;">
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("EANBR") %>' Style="display: none" />
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ENBR") %>' Style="display: none" />
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("EAGuid") %>' Style="display: none" />

                    試區名稱:<asp:Label ID="Label5" runat="server" Text='<%# Eval("EASubject") %>' /><br />
                    描述:<asp:Label ID="Label6" runat="server" Text='<%# Eval("EADescription") %>' /><br />
                    Keyword :<asp:Label ID="Label7" runat="server" Text='<%# Eval("EAKeyword") %>' /><br />
                    單位:<asp:Label ID="Label8" runat="server" Text='<%# Eval("EADepName") %>' /><br />
                    地址:<asp:Label ID="Label9" runat="server" Text='<%# Eval("EAAddress") %>' /><br />
                    是否為學校:<asp:Label ID="Label10" runat="server" Text='<%# Eval("EAIsSchool")=="Y"?"是":"否" %>' /><br />
                    單位保留名額:<asp:Label ID="Label11" runat="server" Text='<%# Eval("EAReservedSeats") %>' /><br />
                    監場主任數:<asp:Label ID="Label12" runat="server" Text='<%# Eval("EPeople2") %>' /><br />
                    監場員數:<asp:Label ID="Label13" runat="server" Text='<%# Eval("EPeople3") %>' /><br />
                    <asp:Button ID="Button1" runat="server" CommandName="Delete" Text="刪除" />
                    <asp:Button ID="Button2" runat="server" CommandName="Edit" Text="編輯" />
                </td>
            </AlternatingItemTemplate>
            <EditItemTemplate>
                <td runat="server" style="background-color: #008A8C; color: #FFFFFF;">
                    <asp:Label ID="EANBRLabel" runat="server" Text='<%# Bind("EANBR") %>' Style="display: none" ></asp:Label>
                    試區名稱:<asp:TextBox ID="EASubjectTextBox" runat="server" Text='<%# Bind("EASubject") %>' /><br />
                    描述:<asp:TextBox ID="EADescriptionTextBox" runat="server" Text='<%# Bind("EADescription") %>' /><br />
                    Keyword:<asp:TextBox ID="EAKeywordTextBox" runat="server" Text='<%# Bind("EAKeyword") %>' /><br />
                    單位:<div class="input-group">
                            <asp:Label ID="EADepIDLabel" runat="server" Text='<%# Bind("EADepID") %>' Style="display: none" ></asp:Label>
                            <asp:TextBox ID="EADepNameTextBox" runat="server" Text='<%# Bind("EADepName") %>'  placeholder="所屬單位" readonly="true"></asp:TextBox>
                            <div class="input-group-btn">
                                <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl="../Account/DepList.aspx?name=&value=" CssClass="btnColorBox"><i class="btn btn-info fa fa-search"></i></asp:HyperLink>
                            </div>
                        </div><br />
                    地址:<asp:TextBox ID="EAAddressTextBox" runat="server" Text='<%# Bind("EAAddress") %>' /><br />
                    是否為學校:<asp:TextBox ID="EAIsSchoolTextBox" runat="server" Text='<%# Bind("EAIsSchool") %>' /><br />
                    單位保留名額:<asp:TextBox ID="EAReservedSeatsTextBox" runat="server" Text='<%# Bind("EAReservedSeats") %>'   TextMode="Number" Min="0"  pattern="\d*"/><br />
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                </td>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px;">
                    <tr>
                        <td>未傳回資料。</td>
                    </tr>
                </table>
            </EmptyDataTemplate>
            <EmptyItemTemplate>
                <td runat="server" />
            </EmptyItemTemplate>
            <GroupTemplate>
                <tr id="itemPlaceholderContainer" runat="server">
                    <td id="itemPlaceholder" runat="server"></td>
                </tr>
            </GroupTemplate>
            <ItemTemplate>
                <td runat="server" style="background-color: #DCDCDC; color: #000000;">
                    <asp:Label ID="Label_EANBR" runat="server" Text='<%# Bind("EANBR") %>' Style="display: none" />
                    <asp:Label ID="Label_ENBR" runat="server" Text='<%# Bind("ENBR") %>' Style="display: none" />
                    <asp:Label ID="Label_EAGuid" runat="server" Text='<%# Bind("EAGuid") %>' Style="display: none" />

                    試區名稱:<asp:Label ID="EASubjectLabel" runat="server" Text='<%# Eval("EASubject") %>' /><br />
                    描述:<asp:Label ID="EADescriptionLabel" runat="server" Text='<%# Eval("EADescription") %>' /><br />
                    Keyword :<asp:Label ID="EAKeywordLabel" runat="server" Text='<%# Eval("EAKeyword") %>' /><br />
                    單位:<asp:Label ID="EADepNameLabel" runat="server" Text='<%# Eval("EADepName") %>' /><br />
                    地址:<asp:Label ID="EAAddressLabel" runat="server" Text='<%# Eval("EAAddress") %>' /><br />
                    是否為學校:<asp:Label ID="Label1" runat="server" Text='<%# Eval("EAIsSchool")=="Y"?"是":"否" %>' /><br />
                    單位保留名額:<asp:Label ID="EAReservedSeats" runat="server" Text='<%# Eval("EAReservedSeats") %>' /><br />
                    監場主任數:<asp:Label ID="EPeople2Label" runat="server" Text='<%# Eval("EPeople2") %>' /><br />
                    監場員數:<asp:Label ID="EPeople3Label" runat="server" Text='<%# Eval("EPeople3") %>' /><br />
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="刪除" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編輯" />
                </td>
            </ItemTemplate>
            <LayoutTemplate>
                <table runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table id="groupPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF; border-collapse: collapse; border-color: #999999; border-style: none; border-width: 1px; font-family: Verdana, Arial, Helvetica, sans-serif;">
                                <tr id="groupPlaceholder" runat="server">
                                </tr>
                            </table>
                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="text-align: center; background-color: #CCCCCC; font-family: Verdana, Arial, Helvetica, sans-serif; color: #000000;">
                            <asp:DataPager ID="DataPager1" runat="server" PageSize="12">
                                <Fields>
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                    <asp:NumericPagerField />
                                    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" ShowNextPageButton="False" ShowPreviousPageButton="False" />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>
            </LayoutTemplate>
            <SelectedItemTemplate>
                <td runat="server" style="background-color: #008A8C; font-weight: bold; color: #FFFFFF;">
                    <asp:Label ID="Label_EANBR" runat="server" Text='<%# Bind("EANBR") %>' Style="display: none" />
                    <asp:Label ID="Label_ENBR" runat="server" Text='<%# Bind("ENBR") %>' Style="display: none" />
                    <asp:Label ID="Label_EAGuid" runat="server" Text='<%# Bind("EAGuid") %>' Style="display: none" />

                    試區名稱:<asp:Label ID="EASubjectLabel" runat="server" Text='<%# Eval("EASubject") %>' /><br />
                    描述:<asp:Label ID="EADescriptionLabel" runat="server" Text='<%# Eval("EADescription") %>' /><br />
                    Keyword :<asp:Label ID="EAKeywordLabel" runat="server" Text='<%# Eval("EAKeyword") %>' /><br />
                    地址:<asp:Label ID="EAAddressLabel" runat="server" Text='<%# Eval("EAAddress") %>' /><br />
                    是否為學校:
                    <asp:Label ID="EAIsSchoolLabel" runat="server" Text='<%# Eval("EAIsSchool")=="Y"?"是":"否" %>' /><br />
                    保留名額:<asp:Label ID="EAReservedSeatsLabel" runat="server" Text='<%# Eval("EAReservedSeats") %>' /><br />
                    監場主任數:<asp:Label ID="EPeople2Label" runat="server" Text='<%# Eval("EPeople2") %>'  TextMode="Number" Min="0"  pattern="\d*" /><br />
                    監場員數:<asp:Label ID="EPeople3Label" runat="server" Text='<%# Eval("EPeople3") %>'  TextMode="Number" Min="0"  pattern="\d*" /><br />
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="刪除" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="編輯" />
                </td>
            </SelectedItemTemplate>
        </asp:ListView>
        <asp:SqlDataSource ID="SqlDataSource_ExamArea" runat="server"
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            DeleteCommand="DELETE FROM [ExamArea] WHERE [EANBR] = @EANBR"
            SelectCommand="SELECT * FROM [ExamArea] WHERE ([ENBR] = @ENBR)"
            UpdateCommand="UPDATE [ExamArea] SET EADepID=@EADepID,EADepName=@EADepName,EAReservedSeats=@EAReservedSeats [EASubject] = @EASubject, [EADescription] = @EADescription, [EAAddress] = @EAAddress, [EAIsSchool] = @EAIsSchool, [EAKeyword] = @EAKeyword WHERE [EANBR] = @EANBR">
            <DeleteParameters>
                <asp:Parameter Name="EANBR" Type="Int32" />
            </DeleteParameters><SelectParameters>
                <asp:QueryStringParameter Name="ENBR" QueryStringField="eid" Type="Int32" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="EASubject" Type="String" />
                <asp:Parameter Name="EADescription" Type="String" />
                <asp:Parameter Name="EAAddress" Type="String" />
                <asp:Parameter Name="EAIsSchool" Type="String" />
                <asp:Parameter Name="EAKeyword" Type="String" />
                <asp:Parameter Name="EADepID" Type="Int32" />
                <asp:Parameter Name="EADepName" Type="String" />
                <asp:Parameter Name="EAReservedSeats" Type="Int32" />
                <asp:Parameter Name="EANBR" Type="Int32" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </ContentTemplate>
</asp:UpdatePanel>

aspx.cs


protected void btEASerach_Click(object sender, EventArgs e)
{
    List<string> sqlWhere = new List<string>() { "[ENBR] = @ENBR" };// "[ENBR] = @ENBR"
    if (!string.IsNullOrEmpty(EASubject.Text))
        sqlWhere.Add("EASubject like '%" + myString.SQLFilterTextBox(EASubject.Text) + "%'");

    if (!string.IsNullOrEmpty(EAAddress.Text))
        sqlWhere.Add("EAAddress like '%" + myString.SQLFilterTextBox(EAAddress.Text) + "%'");

    //if (!string.IsNullOrEmpty(EADep.Text))
    //    sqlWhere.Add("EADep='" + myString.SQLFilterTextBox(EADep.Text) + "'");

    if (!string.IsNullOrEmpty(EAReservedSeats.Text))
        sqlWhere.Add("EAReservedSeats='" + myString.SQLFilterTextBox(EAReservedSeats.Text) + "'");

    if (!string.IsNullOrEmpty(EAIsSchool.SelectedValue))
        sqlWhere.Add("EAIsSchool='" + myString.SQLFilterTextBox(EAIsSchool.SelectedValue) + "'");

    if (sqlWhere.Count > 0)
    {
        SqlDataSource_ExamArea.SelectCommand = "SELECT * FROM [ExamArea] WHERE " + string.Join(" and ", sqlWhere);
    }
    SqlDataSource_ExamArea.DataBind();
    ListView_ExamArea.DataBind();
}

/*
 * ListView切換編輯、刪除、更新、取消的生命週期
    ListView_ExamArea_Init
    ListView_ExamArea_LayoutCreated
    ListView_ExamArea_ItemCreated  x n
    ListView_ExamArea_Load
    ListView_ExamArea_ItemCommand
    ListView_ExamArea_ItemCreated  x n
    ListView_ExamArea_PreRender
    ListView_ExamArea_Unload
*/

protected void ListView_ExamArea_PreRender(object sender, EventArgs e)
{

    // 重新上colorbox,確保colorbox運作正常
    if (ScriptManager.GetCurrent(this.Page) == null)
    {
        Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "msg", "$('.btnColorBox').colorbox({ iframe: true, innerWidth: iwidth, innerHeight: iheight });", true);
    }
    else
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "$('.btnColorBox').colorbox({ iframe: true, innerWidth: iwidth, innerHeight: iheight });", true);
    }
}

protected void ListView_ExamArea_ItemCommand(object sender, ListViewCommandEventArgs e)
{
    ListView listView = sender as ListView;
    ExamDB db = new ExamDB();
    switch (e.CommandName)
    {
        // 不知為啥內建更新會沒動作,所以另外寫
        case "Update":
            var EANBR = 0;
            int.TryParse(((Label)e.Item.FindControl("EANBRLabel")).Text, out EANBR);
            var EASubject = myString.SQLFilterTextBox(((TextBox)e.Item.FindControl("EASubjectTextBox")).Text);
            var EADescription = ((TextBox)e.Item.FindControl("EADescriptionTextBox")).Text;
            var EAKeyword = ((TextBox)e.Item.FindControl("EAKeywordTextBox")).Text;
            var EADepID = 0;
            int.TryParse(((Label)e.Item.FindControl("EADepIDLabel")).Text, out EADepID);
            var EADepName = ((TextBox)e.Item.FindControl("EADepNameTextBox")).Text;
            var EAAddress = ((TextBox)e.Item.FindControl("EAAddressTextBox")).Text;
            var EAIsSchoolTextBox = ((TextBox)e.Item.FindControl("EAIsSchoolTextBox")).Text; // @@
            var EAReservedSeats = 0;
            int.TryParse(((TextBox)e.Item.FindControl("EAReservedSeatsTextBox")).Text, out EAReservedSeats);

            ExamArea ea  = db.ExamArea.Where(p => p.EANBR == EANBR).SingleOrDefault();
            if (ea != null)
            {
                ea.EASubject = EASubject;
                ea.EADescription = EADescription;
                ea.EAKeyword = EAKeyword;
                ea.EADepID = EADepID;
                ea.EADepName = EADepName;
                ea.EAAddress = EAAddress;
                ea.EAIsSchool = EAIsSchoolTextBox;
                ea.EAReservedSeats = EAReservedSeats;

                db.SaveChanges();
            }
            //listView.DataBind();
            //Response.Redirect(Request.Url.ToString());
            //Response.AddHeader("Refresh","0" );

            break;
        default:
            break;
    }
}


protected void ListView_ExamArea_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        // 編輯狀態 單位的colorbox連結
        var HyperLink2 = (HyperLink)e.Item.FindControl("HyperLink2");
        if (HyperLink2 != null)
        {
            var EADepIDLabel = (Label)e.Item.FindControl("EADepIDLabel");
            var EADepNameTextBox = (TextBox)e.Item.FindControl("EADepNameTextBox");
            HyperLink2.NavigateUrl = "../Account/DepList.aspx?name=" + EADepNameTextBox.ClientID + "&value=" + EADepIDLabel.ClientID;
        }
    }
}

找到原因了...UpdateCommand少打一個逗號...XD

player iT邦大師 1 級 ‧ 2020-06-09 00:07:27 檢舉
有一個ListView,使用內建的編輯、刪除,但編輯進入後更新按鈕沒有任何反應?
你是不是忘了接事件?
還有AJAX很難Debug的, 請先把UpdatePanel 拆掉
舜~ iT邦高手 1 級 ‧ 2020-06-09 01:19:22 檢舉
內建的更新是自動透過SqlDataSource自行動作,完成後自行回到檢視狀態,無需寫任何事件...但不知哪邊出錯所以才另外寫ItemCommand...
AJAX很難Debug的, 請先把UpdatePanel 拆掉...確實忘了這個,感謝提醒
舜~ iT邦高手 1 級 ‧ 2020-06-09 22:23:20 檢舉
找到原因了...UpdateCommand少打一個逗號...XD
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答