简体   繁体   English

asp.net c# 中 GridView 内的空文本框值

[英]Empty Textbox values inside a GridView in asp.net c#

I have this GridView and and on a button click want to send the whole gridview data in an email body (using SMTP)我有这个 GridView 并且单击按钮想要在 email 正文中发送整个 gridview 数据(使用 SMTP)

<asp:GridView ID="GridView2" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
    <asp:TemplateField HeaderText="Heading 1">
        <ItemTemplate>
            <asp:TextBox ID="tbxHeading1" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Heading 2">
        <ItemTemplate>
            <asp:TextBox ID="tbxHeading2" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Heading 3">
        <ItemTemplate>
            <asp:DropDownList runat="server" ID="ddlHeading3" Height="26">
                <asp:ListItem Text="&nbsp;" Selected="True" />
                <asp:ListItem Text="list-1" />
                <asp:ListItem Text="list-2" />
                <asp:ListItem Text="list-3" />
            </asp:DropDownList>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Heading 4">
        <ItemTemplate>
            <asp:TextBox ID="tbxHeading4" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Heading 5">
        <ItemTemplate>
            <asp:TextBox ID="tbxHeading5" runat="server"></asp:TextBox>
        </ItemTemplate>
    </asp:TemplateField>
</Columns></asp:GridView>

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"></asp:Button>

Button click:按钮点击:

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
   StringBuilder strBuilder = new StringBuilder(); 
   StringWriter strWriter = new StringWriter(strBuilder); 
   HtmlTextWriter htw = new HtmlTextWriter(strWriter); 
   GridView2.RenderControl(htw); 

   message.Body += strBuilder.ToString(); 
}

On button click I am getting the data in the email body but if the textbox value is empty I am getting an empty array.单击按钮时,我在 email 正文中获取数据,但如果文本框值为空,我将获取一个空数组。 Like this Image喜欢这张图片

Is there anyway to hide the empty array "[ ]" if the textbox is empty?如果文本框为空,是否有隐藏空数组“[]”的方法?

NOTE : When I debug the code I am seeing no value attribute to the textbox.注意:当我调试代码时,我没有看到文本框的值属性。 If I passed default value to texbox, no use.如果我将默认值传递给 texbox,则没有用。 Debug code:调试代码:

<td><input name="defaultcontent_0$GridView2$ctl02$tbxHeading1" type="text" id="defaultcontent_0_GridView2_tbxHeading1_0" /></td>

Thanks in advance!提前致谢!

Try to replace it with empty string before passing it to the message body like this在将它传递给消息正文之前尝试用空字符串替换它

strBuilder = strBuilder.replace("[]","");

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

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