简体   繁体   中英

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)

<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. 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. 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("[]","");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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