<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductId") %>'>
</asp:TextBox>
</InsertItemTemplate>
How do I pass a value to the Bind("ProductId")? More specifically:
Request.QueryString["ProductId"]
EDIT:
leave it with your original bind but add OnDataBinding
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductId") %>'
OnDataBinding = "TextBox1_OnDataBinding"></asp:TextBox>
and
protected void TextBox1_OnDataBinding(object sender, EventArgs e)
{
(sender as TextBox).Text = Request.QueryString["ProductId"];
}
In addition to
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Request.QueryString["ProductID"]%>' />
You need to handle the OnInserting event of your datasource and set the value there with the querystring parameter.
You will have to explicitly set the TextBox value in the DataBound event of your DataBoundControl (repeater/gv etc).
Related
I have a linkbutton in a Repeater that needs to fire a method in the codebehind when clicked, but the method never executes when the LinkButton is clicked. Here is the HTML for the Repeater:
<asp:Repeater ID="rptFeatures" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("Name") %>'
CommandName="listItem_Click"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "AccessListItemId") %>'>
</asp:LinkButton><br />
</ItemTemplate>
</asp:Repeater>
Here is the method that needs to fire onClick:
protected void listItem_Click(object sender, RepeaterCommandEventArgs e)
{
throw new NotImplementedException();
}
The method never gets called as I have a breakpoint on the NotEmplementedException just to see if it hits. Can someone please tell me what I am doing wrong here?
Any assistance is greatly appreciated!
Try This :
<asp:Repeater ID="rptFeatures" runat="server" OnItemCommand="rptFeatures_OnItemCommand">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("Name") %>'
CommandName="listClick"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "AccessListItemId") %>'>
</asp:LinkButton><br />
</ItemTemplate>
</asp:Repeater>
protected void rptFeatures_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("listItem_Click"))
{
// your code
}
}
Instead of CommandName Use onClick Event.
OnClick="listItem_Click"
I needed to add "OnItemCommand=listItem_ItemCommand" to the Repeater to Baseer Haider's answer. The repeater now looks like this:
<asp:Repeater ID="rptFeatures" runat="server" OnItemCommand="listItem_ItemCommand">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("Name") %>' CommandName="listClick" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "AccessListItemId") %>'></asp:LinkButton><br />
</ItemTemplate>
</asp:Repeater>
Now it hits the method with arguments included:
protected void listItem_ItemCommand (Object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("listItem_Click"))
{
// your code
}
}
<asp:DataList ID="DataList1" runat="server" RepeatColumns="5" RepeatDirection="Horizontal"
onitemcommand="DataList1_ItemCommand"
onselectedindexchanged="DataList1_SelectedIndexChanged"
ondatabinding="DataList1_DataBinding">
<ItemTemplate>
<asp:HiddenField ID="Hdnqid" runat="server" Value='<%# Bind("Id") %>' />
<asp:HiddenField ID="HidnResultStatus" runat="server" Value='<%# Bind("ResultStatus") %>' />
<asp:Button ID="Butto" runat="server" Text='<%#Eval("Id") %>' CommandName="Save&Next"
CommandArgument='<%#Eval("Id") %>' />
</ItemTemplate>
</asp:DataList>
I want to change color of button accourding to value of table.
One way of doing this is using OnDataBound event of the data list. In the event you can get the button as well as your data item properties. Here you can change the properties of the button as below
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
Button button = e.Item.FindControl("Butto") as Button;
HiddenField HidnResultStatus = e.Item.FindControl("HidnResultStatus") as HiddenField;
string property = DataBinder.Eval(e.Item.DataItem, "colorproperty") as string;
//Here you can change the button color based on the value
if(HidnResultStatus.Value=="")
button.ForeColor = System.Drawing.Color.Black;
if(HidnResultStatus.Value=="1")
button.ForeColor = System.Drawing.Color.Brown;
}
I am using DataGrid in Asp.net
<asp:DataGrid>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkDetail" runat="server" OnCheckedChanged="chkDetail_CheckedChanged" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Name" HeaderStyle-Font-Bold="true" HeaderStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
As you can see, there is a Checkbox inside DataGrid.
Everytime I check in checkbox, I would like to fire 2 events
1. Other checkboxes will be unselected (I can do it with JS - so it's ok)
2. Context will store DataGrid Name field like this
protected void chkDetail_CheckedChanged(object sender, EventArgs e)
{
Context["Name"] = ??;
}
Because DataGrid doesn't have "Rows" like GridView, I have no idea how to get the Name from the same row.
Thanks in advance.
DataGridViewRow dgvrows = TrGrid.SelectedRows;
string c = dgvrows.Cells("Late_Time").value.toString();
OR
DataGridViewRow dgvrow = this.dataGridView1.Rows[index];
DataRowView drvrow = (DataRowView)dgvrow.DataBoundItem;
You can try this code
protected void chkDetail_CheckedChanged(object sender, EventArgs e)
{
Context["Name"] = (((sender as CheckBox).NamingContainer as DataGridItem).FindControl("label_id") as Label).Text;
}
Your sender will be a CheckBox as the event is triggered by it.
Every control will have a property called NamingContainer which gives the nearest server control that inherits INamingContainer interface. In your case it is DataGridItem. And you can use FindControl method to find child control and cast it to appropriate control type to access its properties.
I'm having trouble with a DropDownList within a GridView that posts a NULL value when in fact a value is selected from the list when using inline editing.
The problem is that I can't use this method to bind the value to the UpdateCommand in my SqlDataSource:
SelectedValue='<%# Bind("Value") %>'
The reason for this is because the value might not exist in the list so it throws an exception.
Is there a way I can bind the value to the UpdateCommand without using SelectedValue?
Thanks in advanced.
You may use RowDataBound to set SelectedValue;
in .cs file
protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView grid = (GridView)sender;
DropDownList DropDownList1 = (e.Row.FindControl("DropDownList1") as DropDownList);
HiddenField HiddenField1 = (e.Row.FindControl("HiddenField1") as HiddenField);
DropDownList1.SelectedValue = HiddenField1.Value;
}
in .aspx file;
<Columns>
...
<asp:TemplateField HeaderText="Column Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Value") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("Value") %>'></asp:HiddenField>
<asp:DropDownList ID="DropDownList1" runat="server">
...
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
In my ASP.NET application, I have a GridView. For a particular field in this GridView, I've added an EditItemTemplate with a DropDownList. However, if the value of the field is "X", then I want to just display a label instead of the DropDownList. So how can I programatically check the field value, then decide which control to display?
Here is my EditItemTemplate:
<EditItemTemplate>
<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
DataSourceID="ODSTechLvl" DataTextField="Level_Name"
DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'>
</asp:DropDownList>
</EditItemTemplate>
If the value of Level_ID is "X", then I want to use:
<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'></asp:Label>
instead of the DropDownList.
I tried embedding an if statement before the DropDownList to check Eval("Level_ID"), but that doesn't seem to work. Any thoughts?
Try this:
<EditItemTemplate>
<asp:DropDownList ID="DropDownListLevel_ID" runat="server"
DataSourceID="ODSTechLvl" DataTextField="Level_Name"
DataValueField="Level_ID" SelectedValue='<%# Bind("Level_ID", "{0}") %>'
Visible='<%# Eval("Level_ID") != "X" %>'>
</asp:DropDownList>
<asp:Label ID="LabelLevel_ID" runat="server" Text='<%# Bind("Level_ID") %>'
Visible='<%# Eval("Level_ID") == "X" %>'></asp:Label>
</EditItemTemplate>
Here is something that will work for ASP.Net.
You could create an RowDataBound event and hide the label or the DropDownList
<asp:GridView id="thingsGrid" runat="server" OnRowDataBound="thingsGrid_RowDataBound"
... >
...
and in your code behind:
protected void thingsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var boundData = e.Row.DataItem;
...
if (boundDataMeetsCondition)
{
e.Row.Cells[4].FindControl("editThingDropDownList").Visible = false;
e.Row.Cells[4].FindControl("editThingLabel").Visible = true;//*
}
else
{
...
}
}
}
*note this is less than ideal because it hard codes the cell index, and the ID of the controls is a string that wont be checked until runtime. There are much more elegant ways to solve this problem in asp.net mvc.
the OnRowDataBound is a sledge hammer that will give you full access to your grid, page methods, and your entire application. In a very simple scenario you could also do it inline without involving the codebehind.
<asp:Label ID="Label1" runat="server" Visible='<%# Convert.ToBoolean(Eval("BooleanPropertyInData"))%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>
or
<asp:Label ID="Label1" runat="server" Visible='<%# Eval("PropertyInData").ToString()=="specialValue"%>' Text='<%# Eval("PropertyInData") %>'></asp:Label>
in the first inline approach, your data source has to expose such a property, and in the second you are hard coding your specialValue business logic into your presentation, which is also ugly and will lead to problems with maintainability.