在不向数据库表添加数据的情况下阻止用户继续

时间:2015-11-17 02:39:51

标签: c# asp.net

我在ASP.net中创建了一个使用MultiView控件的注册向导,其中有多个View控件。下面可以看到一个View控件的示例;

<asp:View ID="address_view" runat="server">
    <h1>Address:</h1>
<asp:Button ID="add_new_address" CssClass="blue" runat="server" Text="Add New Address" OnClick="add_new_address_Click" />
<div id="add_address_div" runat="server">
    <asp:DropDownList ID="address_dropdown_insert" runat="server">
                <asp:ListItem Value="Home">Home Address</asp:ListItem>
                <asp:ListItem Value="Term">Term Time Address</asp:ListItem>
                <asp:ListItem Value="Mail">Mail Address</asp:ListItem>
                <asp:ListItem Value="Business">Business Address</asp:ListItem>
            </asp:DropDownList><br />
            Address 1:
            <asp:TextBox Text="" runat="server" ID="address_1TextBox" /><br />
            Address 2:
            <asp:TextBox Text="" runat="server" ID="address_2TextBox" /><br />
            Town/City:
            <asp:TextBox Text="" runat="server" ID="town_cityTextBox" /><br />
            County:
            <asp:TextBox Text="" runat="server" ID="countyTextBox" /><br />
            PostCode:
            <asp:TextBox Text="" runat="server" ID="postcodeTextBox" /><br />
            Country:
            <asp:TextBox Text="" runat="server" ID="countryTextBox" />
            <asp:Button runat="server" CommandName="Insert" Text="Insert" ID="InsertButton" OnClick="insert_address_button_Click" />
            <asp:Button runat="server" CommandName="Cancel" Text="Clear" ID="Button4" OnClick="cancel_address_button_Click" /><br />
</div>
    <asp:ListView ID="address_list" runat="server" DataSourceID="user_address" DataKeyNames="address_id">
        <EditItemTemplate>
            // code here
        </EditItemTemplate>
        <EmptyDataTemplate>
            <br />
            <p>There are currently no addresses found, please click the button above to add a new address.</p>
        </EmptyDataTemplate>
        <ItemTemplate>
            //code here
        </ItemTemplate>
        <LayoutTemplate>
            //code here
        </LayoutTemplate>
    </asp:ListView>

<br />
    <asp:Button CommandName="NextView" ID="Button1" runat="server" Text="Next" />
</asp:View>

插入表单插入的数据库表名为address。它有以下字段

address_id
user_id
address_type
address_1
address_2
town_city
county
postcode
country

从另一个名为user_id的表中提取users

我有以下C#来获取当前登录用户的user_id;

//get id of logged in user
        string user_id = Session["user_id"].ToString();

我的问题是,如果用户点击Button1 user_id未显示在address表中,我该如何阻止用户继续浏览下一个视图?

我对C#很新,所以任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

在演示文稿中,您需要在输入地址之前禁用Button1。最初你的Button1标记将包括Enabled =“false”

<asp:Button CommandName="NextView" Enabled="false" ID="Button1" runat="server" Text="Next" />

然后在insert_address_button_Click处理程序中,在成功添加地址后,启用按钮,如下所示:

protected void insert_address_button_Click(object sender, EventArgs e) 
{
    // ..insert the address. If successful, enable the "Next" button
    Button1.Enabled = true; 
}

这是一个简单的方法,但应该按照你的要求做。

修改 要解决Button1在用户中的状态,请返回地址视图:

protected void Page_Load(object sender, EventArgs e)
{
    Button1.Enabled = address_list.Items.Any();
}