I'm using asp and have some textboxes where I want to set the value from code behind. The code below is wrapped inside an asp:DetailsView.
The Textbox I want to get and set value of is InsertItemTemplate with ID=strPositionsName
<asp:TemplateField HeaderText="Name" SortExpression="strPositionName">
<InsertItemTemplate>
<asp:TextBox ID="strPositionName" Width="380px" MaxLength="49" runat="server" Text='<%# Bind("strPositionName") %>'></asp:TextBox>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="Textbox1" Width="380px" MaxLength="49" runat="server" Text='<%# Bind("strPositionName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Width="380px" Text='<%# Bind("strPositionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I managed to get the value by:
var testName = ((TextBox)DetailsView1.FindControl("strPositionName")).Text;
So I tried using this:
((TextBox)DetailsView1.FindControl("strPositionName")).Text = "textboxvalue";
But it didn't work
Try Following Code
TextBox _txtPositionName=(TextBox)DetailsView1.FindControl("strPositionName");
if(_txtPositionName!=null)
_txtPositionName.Text="textboxvalue";
Related
I have one Gridview with 5 columns including the edit column. When I click Edit I would like the last column EditItemTemplate Textbox 'The_Title' to take up the maximum width of the cell. It never does! (I can shrink the tbTitle with the Width attribute but not increase) I have tried many things including 'GridView1_RowDataBound'. What am I missing.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 0; i < e.Row.Cells.Count - 1; i++)
{
if (i == 4)
{
e.Row.Cells[i].Attributes.Add("style", "white-space:nowrap;padding:0px;margin:0px;width:500px");
}
}
}
GridView .aspx - only thing on the page no update panel (1 GV and 1 SqlDataSouce).
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Width="1000px" OnRowEditing="GridView1_RowEditing" OnPreRender="GridView1_PreRender" OnRowDataBound="GridView1_RowDataBound" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating">
<EditRowStyle BackColor="Red" Width="100%" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="id" InsertVisible="False" SortExpression="id">
<EditItemTemplate>
<asp:Label ID="lblID1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblID2" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="The_Source" SortExpression="The_Source">
<EditItemTemplate>
<asp:TextBox ID="tbSrc" runat="server" Text='<%# Bind("The_Source") %>' Width="50px" ></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSrc" runat="server" Text='<%# Bind("The_Source") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Section" SortExpression="Section">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Section") %>' Width="50px"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("Section") %>' Width="50px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="The_Title" SortExpression="The_Title" HeaderStyle-Width="500px" HeaderStyle-BackColor="Red">
<EditItemTemplate>
<div style="white-space:nowrap;background-color:blue;text-align:center;padding:0px;margin:0px;">
<asp:TextBox ID="tbTitle" runat="server" Text='<%# Bind("The_Title") %>' Width="500px" style="white-space:nowrap;"></asp:TextBox>
</div>
</EditItemTemplate>
<ItemTemplate >
<asp:Label ID="lblTitle" runat="server" Text='<%# Bind("The_Title") %>' Width="500px"></asp:Label>
</ItemTemplate>
<ControlStyle BackColor="#3399FF" />
</asp:TemplateField>
</Columns>
</asp:GridView>
// Result image
Any clues would be appreciated. Thank You.
Using Browser developer tools, you should be able to inspect the resulting mark-up (HTML) for your text box and tinker with it to see what is causing the size restriction.
IE developer tools (F12) will show you the hierarchical (cascading) order where the width setting is being defined.
Tested your GridView. The TextBox tbTitle is 500 px wide, just as expected. There is probably some bootstrap or other css overwriting the width.
What happens if you add a class for it?
<EditItemTemplate>
<asp:TextBox ID="tbTitle" runat="server" CssClass="fullWidth" Text='<%# Bind("The_Title") %>'></asp:TextBox>
</EditItemTemplate>
.fullWidth {
width: 100% !important;
}
Or set it inline as !important. If that does not work then there is probably something else going on.
<asp:TextBox ID="tbTitle" runat="server" style="width: 100% !important" Text='<%# Bind("The_Title") %>'></asp:TextBox>
I want to make one of the data bound in my gridview uneditable or in my case, I want to make the textbox in edit mode in read only. here is what I have tried but not successful:
TextBox ProductImage = GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
ProductImage.ReadOnly = true;
and here is the aspx code:
asp:TemplateField HeaderText="ProductImage" SortExpression="ProductImage">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("ProductImage") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ProductImage") %>' />
</ItemTemplate>
<ControlStyle Width="50px" />
</asp:TemplateField>
can someone help me out?
Have you tried setting ReadOnly to true in your aspx code?
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true" Text='<%# Eval("ProductImage") %>'/>
</EditItemTemplate>
Or you could use a Label instead of a TextBox
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ProductImage") %>'/>
I am getting this ~/Data/TestCopy.pdf for my Documents in the GridView but I am trying to get only the filename without the directory from SQL database. Does anyone know how to achieve this?
Thanks!
<asp:TemplateField HeaderText="Documents " SortExpression="filePath">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("filePath") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Bind("filePath") %>'
NavigateUrl='<%# Eval("filePath") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
The simplest way is, just add the Reference to System.IO and use Path class to get only the file name, little change in your code,
<asp:TemplateField HeaderText="Documents " SortExpression="filePath">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Path.GetFileName(Bind("filePath")) %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Target="_blank" runat="server" Text='<%# Path.GetFileName(Bind("filePath")) %>'
NavigateUrl='<%# Eval("filePath") %>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
In Gridview RowDataBound , evaluate filename from filepath.
System.IO.Path.GetFileName(filePath).
Not sure but you can try something like this:-
Text='<%# Eval("filePath").ToString().Split(Eval("filePath").ToString().LastIndexOf("/"))[1] %>'
May be you have stored it this way ~/Data/TestCopy.pdf in the database. You can use split function to split the last '/'
Not able to DataBound the Style attribute
Style='<%# Eval("LeftPadding","padding-left:{0}") %>'
Full Code
<asp:TemplateField HeaderText="Report Item" SortExpression="ReportItem">
<ItemTemplate>
<asp:Label Style='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="350px" />
</asp:TemplateField>
But I can DataBound some other attributes even not a Standard HTML attribute, like below
<asp:Label StyleTemp='<%# Eval("LeftPadding","padding-left:{0}") %>' ID="lblReportItem"
runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
What is the problem with Style ?
Got Answer :)
<asp:Label Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>
ID="lblReportItem" runat="server" Text='<%# Eval("Caption") %>'></asp:Label>
Tried by without giving the Single Quotes for the style attribute and used string.Format
Style=<%# string.Format("padding-left:{0}px",Eval("LeftPadding")!=DBNull.Value? Convert.ToString(Eval("LeftPadding")): "0") %>
I have a grid view.In this i want balance= total paid leave-paid leave taken
what shoul i do
<asp:TemplateField HeaderText="Total Paid Leaves" SortExpression="Location_name">
<EditItemTemplate>
<asp:TextBox ID="txttotal_paid_leaves" runat="server" Text='<%# Eval("total_paid_leaves") %>'>
</asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewtotal_paid_leaves" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("total_paid_leaves") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Paid Leaves taken">
<EditItemTemplate>
<asp:TextBox ID="txtpaid_leaves_taken" runat="server" Text='<%# Bind("paid_leaves_taken") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewpaid_leaves_taken" runat="server">
</asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblpaid_leaves_taken" runat="server" Text='<%# Bind("paid_leaves_taken") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Balance">
<ItemTemplate>
----Please guide me in this part----
</ItemTemplate>
</asp:TemplateField>
you can try like this
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Convert.ToInt32(Eval("TotalLeave")) -Convert.ToInt32(Eval("LeaveTaken")) %>'></asp:Label>
</ItemTemplate>
Why don't you do that calculation in sql? That's better and easy to manage.
for eample
SELECT TOTAL_LEAVE, LEAVE_TAKEN, (TOTAL_LEAVE - LEAVE_TAKEN) BALANCE
FROM TABLE
WHERE .......