简体   繁体   English

asp.net webform vb 在下拉列表中添加新记录

[英]asp.net webform vb add new record in a dropdownlist

I Have A Asp.net DropDownList return Data from a 1st table and insert to 2nd table we are inserting Name, Age and position to the 2nd table which the position is returning data from 1st table.我有一个 Asp.net DropDownList 从第一个表返回数据并插入到第二个表,我们将姓名、年龄和 position 插入到 Z4757FE07FD492A8BE0EA6A760D683D6 表返回的第二个表中。 the question is where there was a new record we will first add in the 1st table then refresh the page to be able to get the new record.问题是哪里有新记录,我们将首先在第一个表中添加,然后刷新页面以获取新记录。 is there is a way when click on dropdownlist at the end of the existing record will be a button "add new" and when click open a popup add new record after that the record add in a 1st table and also be available in the dropdownlist without refresh the page.有没有办法在现有记录末尾单击下拉列表时将出现“添加新”按钮,当单击打开弹出窗口时添加新记录,然后将记录添加到第一个表中,并且在下拉列表中也可用刷新页面。 is this possible?这可能吗?

Everything up to the issue of NOT wanting to re-fresh the page was great.关于不想重新刷新页面的问题的一切都很棒。

How big of a deal is a page post back?页面回帖有多大意义? I mean if you have several buttons on that form, then the page should be able to tolerate a post back.我的意思是,如果您在该表单上有多个按钮,那么该页面应该能够容忍回发。 The reason for this issue/question/point is that WHEN you can allow post-backs, then you have a LOT of options and options that allow a lot of freedom to shove in a dialog pop-up that lets you add a new value to the combo box.这个问题/问题/观点的原因是,当您可以允许回发时,您有很多选项和选项可以让您在对话框弹出窗口中自由推挤,让您添加新的价值组合框。 And keep in mind that pop option to add a new row to the combo box (dropdownlist) will have to be re-bound with the just newly added row.请记住,将新行添加到组合框(下拉列表)的弹出选项必须与刚刚添加的行重新绑定。

If push comes to shove and you REALLY can't tolrate the postback, then you can place the combo box and the "add" to combo box in a update panel.如果迫在眉睫并且您真的无法容忍回发,那么您可以将组合框和“添加”到组合框放在更新面板中。

Next up: Because we want to pop a dialog?接下来:因为我们想弹出一个对话框? I would recommend adopting jQuery.UI.我建议采用 jQuery.UI。 You have a good chance that jQuery is already included in your project, and thus adopting jQuery.UI is not much more.您很有可能 jQuery 已经包含在您的项目中,因此采用 jQuery.UI 并没有太多。

There are bootstrap dialogs also - but they are really difficult to work with.也有引导对话框 - 但它们真的很难使用。

So, lets lay out how this will work.所以,让我们来说明这将如何工作。

So, assume we have a little bit of markup a few text boxes, and that dropdown.因此,假设我们对一些文本框和那个下拉菜单进行了一些标记。

Say this:说这个:

<div style="padding:5px;float:left;width:250px;text-align:right">
    Hotel Name: <asp:TextBox ID="txtHotelName" runat="server"  Width="130px" />
    <br />
    First Name: <asp:TextBox ID="txtFirst" runat="server"  Width="130px" />
    <br />
    Last Name: <asp:TextBox ID="txtLast" runat="server" Text ='<%# Eval("LastName") %>'  Width="130px" />
    <br />

    <div>
    City : <asp:DropDownList ID="cboCity" runat="server" DataTextField="City" 
        DataValueField="City" Width="110px">
        </asp:DropDownList>
        <div style="float:right;text-align:center;margin-left:4px;margin-top:-1px">
        <asp:Button ID="cmdAddCity" runat="server" Text="+" 
                OnClientClick="AddCity();return false"
                style="padding-left:4px;padding-right:8px;width:12px;height:16px;Font-Size:8px"
            />
        </div>
    </div>
    Active: <asp:CheckBox ID="chkActive" runat="server" Checked = '<%# Eval("Active") %>'/>
</div>

Not much to above, and we get this:上面没有太多,我们得到这个:

在此处输入图像描述

Of course on page load, I filled the drop list with code.当然,在页面加载时,我用代码填充了下拉列表。 I have this:我有这个:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If IsPostBack = False Then

        LoadCombo

    End If

End Sub

Sub LoadCombo()

    Using cmdSQL As New SqlCommand("SELECT City from tblCity ORDER BY City", New SqlConnection(My.Settings.TEST3))

        cmdSQL.Connection.Open()
        cboCity.DataSource = cmdSQL.ExecuteReader
        cboCity.DataBind()

    End Using

End Sub

As noted, we do tend to write statup + load up code in the page load event, but note how I test for ONLY the first post back.如前所述,我们确实倾向于在页面加载事件中编写 statup + load up 代码,但请注意我如何仅测试第一个回帖。 Thus, you can quite much write pages that can "survie" a post back.因此,您可以编写很多可以“浏览”回帖的页面。

Ok, so now we need a popup for the jQuery.UI.好的,所以现在我们需要一个 jQuery.UI 的弹出窗口。 These are nice, since we don't have to create a whole new web page.这些很好,因为我们不必创建一个全新的 web 页面。 The markup for this pop up form (a div) in the current page can/will look like this:当前页面中此弹出表单(一个 div)的标记可以/将如下所示:

   <div id="CityDialog" style="display:Normal" title="Add New City">
        <br />
        <asp:TextBox ID="txtNewCity" runat="server" Placeholder="Enter New City" ClientIDMode="Static" ></asp:TextBox>
        <br />
        <br />

        <asp:Button ID="cmdOkAddCity" runat="server" Text="Add" ClientIDMode="Static" />
        <asp:Button ID="cmdCityCancel" runat="server" Text="Cancel" ClientIDMode="Static" 
            OnClientClick="$('#CityDialog').dialog('close');return false;"
            style="margin-left:20px"/>
    </div>

So we add a title to the div.所以我们给 div 添加一个标题。 a text box for the new city to add.要添加的新城市的文本框。 And two simple standard asp.net buttons.和两个简单的标准 asp.net 按钮。

So, we add above to the above page.因此,我们将上面的内容添加到上面的页面中。 and then double click on the cmdOkAdd button, and write the code to add a new row to the combo box.然后双击 cmdOkAdd 按钮,并编写代码以将新行添加到组合框中。

That code looks like this:该代码如下所示:

Protected Sub cmdOkAddCity_Click(sender As Object, e As EventArgs) Handles cmdOkAddCity.Click

    If txtNewCity.Text <> "" Then

        Using cmdSQL As New SqlCommand("INSERT INTO tblCity (City) VALUES (@City)", New SqlConnection(My.Settings.TEST3))

            cmdSQL.Parameters.Add("@City", SqlDbType.NVarChar).Value = txtNewCity.Text
            cmdSQL.Connection.Open()
            cmdSQL.ExecuteNonQuery()

            ' re-load combo
            LoadCombo()

            '
            ' set cbo to the new value we just added
            cboCity.Text = txtNewCity.Text

        End Using

    End If

End Sub

Again, real simple code.再次,真正简单的代码。 (and all easy code behind so far). (以及到目前为止所有简单的代码)。 So what above does is check if the user did enter that new city into the text box, and if yes, then we add the row to the database, and then re-load the combo box and then we SET the combo box to the new city just added.所以上面所做的是检查用户是否确实在文本框中输入了那个新城市,如果是,那么我们将该行添加到数据库中,然后重新加载组合框,然后我们将组合框设置为新的刚刚添加的城市。 This makes for a easy flow, as the user can then click on that little "+" button beside the combo box, and we pop a dialog, and then the user enters the city, and hits the ok button to run above code stub.这使得流程变得简单,因为用户可以点击组合框旁边的那个小“+”按钮,我们会弹出一个对话框,然后用户进入城市,然后点击 ok 按钮运行上面的代码存根。

Ok, now we have to setup the jQuery.UI.好的,现在我们必须设置 jQuery.UI。 yes, this is a WEE bit of JavaScript, but NOT much.是的,这是 JavaScript 的一小部分,但不多。

So, that "+" button beside the drop down?那么,下拉菜单旁边的那个“+”按钮呢? It is a regular asp button, it is going to pop that jQuery.UI dialog.它是一个普通的 asp 按钮,它会弹出 jQuery.UI 对话框。 That button looks like this:该按钮如下所示:

 <asp:Button ID="cmdAddCity" runat="server" Text="+" 
    OnClientClick="AddCity();return false"
   style="padding-left:4px;padding-right:8px;width:12px;height:16px;Font-Size:8px"
    />

Note how we have OnClientClick= - that's going to run our JavaScript code stub.请注意我们如何拥有 OnClientClick= - 这将运行我们的 JavaScript 代码存根。

So, say right after that last tag, we have this code:所以,在最后一个标签之后,我们有这个代码:

</form>
    <script>
        function AddCity() {
            $("#CityDialog").dialog({
                modal: true,
                width: "220px",
                resizable: false,
                appendTo: "form"
            });
        }
    </script>

Not much code.代码不多。 What that code does it pop the "div" as a nice way cool dialog form.该代码的作用是将“div”作为一种很酷的对话框形式弹出。 And it turns the rest of the background into a "disabled gray".并将背景的rest变成“禁用灰色”。

So, it works well.所以,它运作良好。 And last but not least?最后但并非最不重要? We have to hide that dialog div.我们必须隐藏那个对话框 div。 Remember, don't hide it at first, since we need to double click on the Ok button (add city button) to wire up our add event.记住,一开始不要隐藏它,因为我们需要双击 Ok 按钮(添加城市按钮)来连接我们的 add 事件。 Once done, then we hide that div by changing display:normal to Display:none.完成后,我们通过将 display:normal 更改为 Display:none 来隐藏该 div。 Eg this:例如这个:

   <div id="CityDialog" style="display:none" title="Add New City">
        <br />
        <asp:TextBox ID="txtNewCity" runat="server" Placeholder="Enter New City" ClientIDMode="Static" ></asp:TextBox>
        <br />
        <br />

        <asp:Button ID="cmdOkAddCity" runat="server" Text="Add" ClientIDMode="Static" />
  ... etc. etc.

Ok, now with the above?好的,现在有了上面的内容? Well, then we now have this setup:好吧,那么我们现在有了这个设置:

在此处输入图像描述

The code was very helpful but When add all code The popup is not triggering, the second thing How make the dropdown ID connected to the city table ID column not the city column this is my code: Download The Code代码很有帮助,但是当添加所有代码时弹出窗口没有触发,第二件事如何使下拉 ID 连接到城市表 ID 列而不是城市列这是我的代码:下载代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM