ASP.NET v2 - RadGridView - asp.net

I have a grid that I need to add columns to dynamically (programatically).
I have been browsing Telerik forums as well as Google and im not able to find anything. Decompiling GridTemplateColumn shows that there should be ItemTemplate property however my VS throws an error.
Dim col As GridColumn = New GridButtonColumn()
col.UniqueName = stockLocation("LocationID").ToString()
col.HeaderText = stockLocation("Name").ToString()
col.ItemTemplate = ERROR HERE
rgGridCombinations.Columns.Add(col)
I'm trying to create a column that will look like following ASPX code.
<radg:GridTemplateColumn HeaderText="Stock" UniqueName="Stock" Visible="true" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:TextBox ID="tbStock" runat="server" Width="100%" />
</ItemTemplate>
</radg:GridTemplateColumn>
How would I create this?

1) GridColumn does not have ItemTemplate property
2) You were assigning GridButtonColumn. Use GridTemplateColumn instead
Try this
Dim col = As New GridTemplateColumn()
col.UniqueName = "Stock"
col.HeaderText = "Stock"
col.ItemTemplate = <something here> 'should be an ITemplate
rgGridCombinations.Columns.Add(col)
For more information on how to create template, check here

Related

Reference specific asp.net control using vb.net code bhind variable

I have read through many different solutions both here on SO and other sites but this keeps eluding me.
I am trying to reference an asp.net textbox and image button from code behind vb.net.
I have tried this code but the variable tBox says it equal is nothing.
Dim callBtn As ImageButton = CType(sender, ImageButton)
Dim tBox As TextBox
Dim iButton As ImageButton
Dim cNumber As Integer = Convert.ToInt32(callBtn.ID.Substring(11))
tBox = CType(Page.FindControl("TextBox" & cNumber), TextBox)
'The line below is commented out because it did not work either but
was the first one I tried
'tBox = DirectCast(Page.FindControl("TextBox" & cNumber), TextBox)
tBox.Text = "" <--- On this line tBox is nothing
tBox.Visible = False
I have placed a break point on the tBox.Text line and tBox equals nothing.
How can I reference the text box from a variable? I also need to reference and image button as well but need to get this one to work first.
If there is a question on here that exactly answers this please point me to it as I could not find one.
Edit: Update
Here is the code that creates a textbox and image button.
<asp:ImageButton ID="ImageButton1" runat="server" Visible="false" OnClick="removeFile" ImageUrl="~/Images/red-x-md20x20.png" ImageAlign="Top" ToolTip="Click To Remove File" /> 
<asp:TextBox ID="TextBox1" runat="server" Visible="false" Width="300px" />
It turns out that I was not looking for the right Name. Being on a content page I had to reference the master page first.
I used the following:
Dim ctrlNameT As String = "ctl00$ContentPlaceHolderRight$TextBox" & cNumber
This did the trick!

Hyperlink Reference to another column DevExpress Gridview ASP.Net Webform

I'm using a for loop to add columns to the DevExpress ASP.Net WebForms GridView using VB.Net. I was able to get a hyperlink to reference the same column value:
Dim newColumn As New DevExpress.Web.GridViewDataHyperLinkColumn
newColumn.PropertiesHyperLinkEdit.NavigateUrlFormatString = "TrendView.aspx?CurrentID={0}"
I need to programmatically set the hyperlink to another column's value... i.e. column three needs to have a hyperlink that references the column 1 value in the same row. How do you access another column in that row using VB or C# during runtime?
please refer this url to solve your problem
https://www.devexpress.com/Support/Center/Example/Details/E308
change your populate grid logic as
ASPX :
<dx:ASPxGridView ID="ASPxGridView1" runat="server"></dx:ASPxGridView>
CS
protected void Page_Init(object sender, EventArgs e)
{
ASPxGridView1.KeyFieldName = "ID";
ASPxGridView1.DataSource = GetData();
if (!IsPostBack && !IsCallback)
{
PopulateColumns();
ASPxGridView1.DataBind();
}
}
public DataTable GetData()
{
DataTable Table = new DataTable();
Table.Columns.Add("ID", typeof(int));
Table.Columns.Add("ItemName", typeof(string));
Table.Columns.Add("ItemValue", typeof(string));
Table.Rows.Add(1, "A","AA");
Table.Rows.Add(2, "B","BB");
return Table;
}
public void PopulateColumns()
{
GridViewDataTextColumn colID = new GridViewDataTextColumn();
colID.FieldName = "ID";
ASPxGridView1.Columns.Add(colID);
GridViewDataTextColumn srk = new GridViewDataTextColumn();
srk.FieldName = "ItemValue";
ASPxGridView1.Columns.Add(srk);
GridViewDataHyperLinkColumn colItemName = new GridViewDataHyperLinkColumn();
colItemName.FieldName = "ItemValue";
colItemName.PropertiesHyperLinkEdit.NavigateUrlFormatString = "~/details.aspx?Device={0}";
colItemName.PropertiesHyperLinkEdit.TextFormatString = "{0}";
colItemName.PropertiesHyperLinkEdit.TextField = "ItemName";
ASPxGridView1.Columns.Add(colItemName);
}
here column itemName refer to itemValue as url string params
If you want to show hyperlink text based on multiple columns then HyperlinkColumn is not the right approach to implement. It would be better use templates.
You should create DataItemTemplate for the column and there you can use Bind statement to format the display text or Hyperlink url as you want. It is the same approach used in ASP.NET GridView control and work in similar manner with ASPxGridView control.
I suggest you to go through these example and attached sample projects will help you know about the implementation.
ASPxGridView - How to customize HyperLink column
How to use a hyperlink whose argument depends on several cell values in the ASPxGridView
How to customize navigate URL for HyperLink column within a ASPxGridView - This contains sample attached in the answer.
Example: DataItemTemplate for column.
<dxwgv:GridViewDataTextColumn FieldName="ContactName" VisibleIndex="3">
<DataItemTemplate>
<dxe:ASPxComboBox ID="ASPxComboBox1" runat="server" ValueType="System.String" DataSourceID="AccessDataSource1"
TextField="ContactName" ValueField="ContactName" Value='<%#Bind("ContactName")%>' OnSelectedIndexChanged="ASPxComboBox1_SelectedIndexChanged">
<ClientSideEvents SelectedIndexChanged="onSelectedIndexChanged" />
</dxe:ASPxComboBox>
</DataItemTemplate>
</dxwgv:GridViewDataTextColumn>
or
<dx:ASPxHyperLink ID="ASPxHyperLink3" runat="server"
NavigateUrl='<%# string.Format("~/AccountDetail.aspx?CategoryID={0}", Eval("i_Customer")) %>'
Text='<%# string.Format("i_Customer{0}", Eval("i_Customer")) %>' .../>
ASPxGridView - ASPxHyperLink Navigate URL formatting
ASPxGridView - How to set GridViewDataHyperlinkColumn's text and navigate url

Hide / unhide Label, textbox depending on the db results in asp.net

I need to hide /unhide the labels and textbox depending on db results , I tried something like this but it doesnt works, the condition should be like if the db field is empty for that field , then the label associated with that field should hidden (not visible) , following is the code i tried :
<asp:Label ID="lblBirth" Text="DOB:" runat="server" ViewStateMode="Disabled" CssClass="lbl" />
<asp:Label ID="DOB" runat="server" CssClass="lblResult" Visible='<%# Eval("Berth") == DBNull.Value %>'></asp:Label>
Code behind:
protected void showDetails(int makeID)
{// get all the details of the selected caravan and populate the empty fields
DataTable dt = new DataTable();
DataTableReader dtr = caravans.GetCaravanDetailsByMakeID(makeID);
while (dtr.Read())
{
//spec
string value = dtr["Price"].ToString();
lblModel.Text = dtr["model"].ToString();
birthResult.Text = dtr["Berth"].ToString(); }}
To make your aspx version work your control should be data bound to data source that contains "Berth" property. As I can see from code behind, you prefer to use c# to populate controls. In this case you may just do the following:
DOB.Visible = dtr["Berth"] == DBNull.Value;
I think that using data binding is more preferable solution.

How to place a hyperlink field in a web page at runtime?

I am trying to display contents of a folder in a hyperlink. I am using masterpage also. The hyperlinks are not shown into the content page. what to do for that?
I know in windows forms we can use like
TextBox.Location=new Point(100,100);
But how to do in web page...please anybody suggest me..
my coding in page_load is
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
int i = 0;
foreach (FileInfo fi in di.GetFiles())
{
HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
Page.Controls.Add(HL);
Page.Controls.Add(new LiteralControl("<br/>"));
}
}
You can't add it directly to Page.Controls. You have to add it to the ContentPlaceHolder on the page.
Instead of dynamically creating controls, which is rather messy and error-prone, have you considered using an asp:Repeater control and binding the files directly to it? Something like:
<asp:Repeater ID="RepeaterFiles" runat="server">
<ItemTemplate>
<asp:HyperLink runat="server" Text='<%# Container.DataItem %>'
NavigateUrl='<%# String.Format("downloading.aspx?file={0}", Container.DataItem)%>' />
<br />
</ItemTemplate>
</asp:Repeater>
and in code behind:
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/ProjectsUpload"));
RepeaterFiles.DataSource = di.GetFiles();
RepeaterFiles.DataBind();
That way you can use declarative mark-up to control layout and keep the logic in your code-behind.
Put a PlaceHolder control on your page:
<asp:PlaceHolder runat="server" ID="ph" />
In your code behind write like:
HyperLink HL = new HyperLink();
HL.ID = "HyperLink" + i++;
HL.Text = fi.Name;
HL.NavigateUrl = "downloading.aspx?file=" + fi.Name;
ph.Controls.Add(HL);
ph.Controls.Add(new Literal { Text = "<br/>"});
I'm making use of the newly C# 3 feature on that last line to set the Text property.
Have you used the debugger to step through the loop to verify that it processes at least one file?
Instead of adding the links to Page.Controls, you could put a list control on the page, and then add each link within a list item. Then you'd know precisely where on the page they should appear.
Create a Panel or a Label in the Page's Content area, and add your HyperLinks into the Controls collection of the Panel.
(Stepping through the code to check whether the IIS App actually enumerates any files in the directory would help too.)

Setting LinkButton Title in ASP.Net Wizard Sidebar Template

Playing around with customizing the appearance of the Wizard control in ASP.Net, and I've found out how to disable the sidebar buttons using the SideBarTemplate and catching the OnItemDataBound event. All pretty easy. What I want to do now is to modify the text of the rendered LinkButton to prefix the step name with something like ">>" for the current step.
So, in my ItemDataBound event handler for the SideBarList, I have the following code:
Dim stepCurrent As WizardStep = e.Item.DataItem
Dim linkCurrent As LinkButton = e.Item.FindControl("SideBarButton")
If Not stepCurrent Is Nothing Then
Trace.Write("SideBar", "Current Step = " & stepCurrent.Wizard.ActiveStep.Name)
Trace.Write("Sidebar", "Link Button = " & linkCurrent.Text)
linkCurrent.Enabled = False
If stepCurrent.Wizard.ActiveStepIndex = e.Item.ItemIndex Then
linkCurrent.Style.Add(HtmlTextWriterStyle.Color, "#000000")
linkCurrent.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
linkCurrent.Text.Insert(0, ">> ")
End If
End If
However, what I find is the trace output is showing an empty string for the lunkbutton text, but the style changes work.
Am I trying to set the text in the wrong place?
Thanks
I did not find any way to change "SideBarButton" text property that is why I added
another link button control in SelectedItemTemplate to DataList and set visible="fasle" in SideBarButton. SelectedItemTemplate will be used to render item in sidebar for current wizard step.
<ItemTemplate>
<asp:LinkButton ID="SideBarButton" runat="server"/>
</ItemTemplate>
<SelectedItemTemplate>
<asp:LinkButton ID="ActiveSideBarButton" runat="server">
<asp:LinkButton Visible="false" ID="SideBarButton"unat="server"/>
</SelectedItemTemplate>
In OnItemDataBound event do something like
Dim stepCurrent As WizardStep = e.Item.DataItem
If stepCurrent.Wizard.ActiveStepIndex = e.Item.ItemIndex Then
Dim linkCurrent As LinkButton = e.Item.FindControl("ActiveSideBarButton")
linkCurrent.Style.Add(HtmlTextWriterStyle.Color, "#000000")
linkCurrent.Style.Add(HtmlTextWriterStyle.FontWeight, "bold")
LinkCurrent.Text = stepCurrent.Title;
linkCurrent.Text.Insert(0, ">> ")
End If
SideBarButton will not be rendered because of visible="false" and only ActiveSideBarButton for current step will be rendered with parameters you need.

Resources