我想用换行符替换动态 gridview 中的分号

发布于 2024-12-09 05:56:24 字数 4083 浏览 1 评论 0原文

我刚刚开始 VB.NET 编程,并设计了一个简单的网页,其中包含一个下拉列表(从 SQL 动态读取),该下拉列表填充仅显示该颜色的公式的 GridView。它涉及专用涂料的混色配方。公式本身在其中一列中具有用分号分隔的各个部分,我想使用此字符作为换行符,以便每个公式部分显示在 GridView 单元格内的单独行上。我尝试了多种方法来替换、拆分等,但遇到了障碍。我在哪里/如何添加代码来执行此替换?

这是我的代码:隐藏

<body> 
    <form id="form1" runat="server">
        <div> 
            <asp:ImageButton ID="ImageButton1" runat="server" 
                             AlternateText="Home Page" Height="46px" 
                             ImageUrl="~/Images/Block_Tagline.jpg"
                             PostBackUrl="~/Default.aspx" Width="140px" />
            <br />
            <asp:LoginName ID="LoginName" 
                           FormatString="Currently logged in as {0}" 
                           runat="server" 
                           ForeColor="#009933" />
            <br />
            <br />
            <br />
            Choose Your Color:
            <asp:DropDownList ID="DropDownList1" runat="server" 
                              AutoPostBack="true" 
                              OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            <br />
            <br />
            <asp:GridView ID="ColorGridView" runat="server" 
                          Width="100%" BackColor="White" 
                          BorderColor="#E7E7FF" BorderStyle="None" 
                          BorderWidth="1px" CellPadding="3" 
                          GridLines="Horizontal">
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <PagerStyle ForeColor="#4A3C8C" HorizontalAlign="Right"
                            BackColor="#E7E7FF" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                                  ForeColor="#F7F7F7" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" 
                             ForeColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#F7F7F7" /> 
            </asp:GridView>
            <span id="Message" runat="server"/>
            <b></b>
            <br />
            <asp:Button ID="btnEdit" runat="server" 
                        Text="Edit/Add/Delete Colors" 
                        PostBackUrl="~/Edit/EditColors.aspx" />
        </div>
    </form>
</body>

代码:

Partial Class _Default
        Inherits System.Web.UI.Page

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

        If Not IsPostBack Then
            Dim username As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
            DropDownList1.Items.Insert(0, New ListItem("---Select---"))

            FillDropDownList()
        End If
    End Sub

    ' Show data in GridView
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim s As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con As SqlConnection = New SqlConnection(s)

        con.Open()

        Dim cmd As SqlCommand = New SqlCommand("SELECT * from z_md_ColorFormulas where BlankColor='" + DropDownList1.SelectedItem.ToString() + "'", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader()

        ColorGridView.DataSource = dr
        ColorGridView.DataBind()

        dr.Close()
        con.Close()
    End Sub

    Public Sub FillDropDownList()

        Dim s1 As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con1 As SqlConnection = New SqlConnection(s1)

        con1.Open()

        Dim cmd1 As SqlCommand = New SqlCommand("Select BlankColor from z_md_ColorFormulas", con1)
        Dim dr1 As SqlDataReader = cmd1.ExecuteReader()

        While dr1.Read()
            DropDownList1.Items.Add(dr1(0).ToString())
        End While

        dr1.Close()
        con1.Close()
    End Sub

I'm just starting into VB.NET programming, and have designed a simple webpage with a dropdown list (dynamically read from SQL) that populates a GridView showing only that color's formula. It deals with color-mixing formulas for specialized paint. The formulas themselves have it's inidividual parts separated by semi-colons in one of the columns, and I want to use this character as a line-break, so that each formula part is shown on a seperate line within the GridView cell. I have tried numerous ways to Replace, Split, etc, but running into a road-block. Where/how do I add code to perform this replacement?

Here's my code as it sits:

<body> 
    <form id="form1" runat="server">
        <div> 
            <asp:ImageButton ID="ImageButton1" runat="server" 
                             AlternateText="Home Page" Height="46px" 
                             ImageUrl="~/Images/Block_Tagline.jpg"
                             PostBackUrl="~/Default.aspx" Width="140px" />
            <br />
            <asp:LoginName ID="LoginName" 
                           FormatString="Currently logged in as {0}" 
                           runat="server" 
                           ForeColor="#009933" />
            <br />
            <br />
            <br />
            Choose Your Color:
            <asp:DropDownList ID="DropDownList1" runat="server" 
                              AutoPostBack="true" 
                              OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            </asp:DropDownList>
            <br />
            <br />
            <asp:GridView ID="ColorGridView" runat="server" 
                          Width="100%" BackColor="White" 
                          BorderColor="#E7E7FF" BorderStyle="None" 
                          BorderWidth="1px" CellPadding="3" 
                          GridLines="Horizontal">
                <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
                <PagerStyle ForeColor="#4A3C8C" HorizontalAlign="Right"
                            BackColor="#E7E7FF" />
                <SelectedRowStyle BackColor="#738A9C" Font-Bold="True"
                                  ForeColor="#F7F7F7" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" 
                             ForeColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#F7F7F7" /> 
            </asp:GridView>
            <span id="Message" runat="server"/>
            <b></b>
            <br />
            <asp:Button ID="btnEdit" runat="server" 
                        Text="Edit/Add/Delete Colors" 
                        PostBackUrl="~/Edit/EditColors.aspx" />
        </div>
    </form>
</body>

Code Behind:

Partial Class _Default
        Inherits System.Web.UI.Page

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

        If Not IsPostBack Then
            Dim username As String = System.Security.Principal.WindowsIdentity.GetCurrent().Name
            DropDownList1.Items.Insert(0, New ListItem("---Select---"))

            FillDropDownList()
        End If
    End Sub

    ' Show data in GridView
    Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim s As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con As SqlConnection = New SqlConnection(s)

        con.Open()

        Dim cmd As SqlCommand = New SqlCommand("SELECT * from z_md_ColorFormulas where BlankColor='" + DropDownList1.SelectedItem.ToString() + "'", con)
        Dim dr As SqlDataReader = cmd.ExecuteReader()

        ColorGridView.DataSource = dr
        ColorGridView.DataBind()

        dr.Close()
        con.Close()
    End Sub

    Public Sub FillDropDownList()

        Dim s1 As String = WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
        Dim con1 As SqlConnection = New SqlConnection(s1)

        con1.Open()

        Dim cmd1 As SqlCommand = New SqlCommand("Select BlankColor from z_md_ColorFormulas", con1)
        Dim dr1 As SqlDataReader = cmd1.ExecuteReader()

        While dr1.Read()
            DropDownList1.Items.Add(dr1(0).ToString())
        End While

        dr1.Close()
        con1.Close()
    End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最偏执的依靠 2024-12-16 05:56:24

只是为了充实您的答案 user988265 我假设您的意思是使用代码:

<asp:TemplateField HeaderText="Colours">
    <ItemTemplate>
        <%# Eval("Colour").ToString().Replace(";","<br />") %>
    </ItemTemplate>
</asp:TemplateField>

当我需要类似的东西时为我工作。想知道是否可以将其填充到下拉列表中?

Just to flesh out your answer user988265 I assume you meant using the code:

<asp:TemplateField HeaderText="Colours">
    <ItemTemplate>
        <%# Eval("Colour").ToString().Replace(";","<br />") %>
    </ItemTemplate>
</asp:TemplateField>

Worked for me when I needed something similar. Was wondering is it possible to have this populate a drop-down list though?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文