Set Width of GridView TextBox in Edit mode

To modify the width of a TextBox (inside the GridView control) when the GridView is in Edit Mode, use any of the two approaches:

Using ControlStyle

<asp:GridView ID="gvCustom" runat="server">
<
Columns>
<
asp:BoundField DataField="EmpID" HeaderText="EmployeeID" >
<
ControlStyle Width="150" />
</
asp:BoundField>
</Columns>
</
asp:GridView>

Use ControlStyle CSS Class (Recommended)

<style type="text/css">
.cssWdth
{
width: 150px;
}
</style>

and in the GridView

<asp:GridView ID="gvCustom" runat="server">
<
Columns>
<asp:BoundField DataField="EmpID" HeaderText="EmployeeID"
ControlStyle-CssClass="cssWdth" />
</
Columns>
</
asp:GridView>

Check some additional GridView Tips and Tricks over here



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

5 comments:

Phil said...

Thank you so much after spending an hour trying to figure out why my styles weren't working inside the gridview/detailview this fixed it!

Anatoli said...

Thanks a lot! It works fine with TemplateField as well.

Hopecherish said...

Thank you. Useful for me.

Weng said...

Thank you. Very straight forward.

Benjamin said...

Thank You very much!
outstaanding decision