access lable control kept in layout of listview? - asp.net

I have one dropdownlist and one button and listview. Button and dropdownlist is outside listview.
On 1st page load, all the item is displayed. Now user can see based on selection of dropdownlist.
I want to show selected text of dropdownlist in lable of layout control of listview.
I wrote below code but doesn't seem to work correctly.
protected void lvProduct_LayoutCreated(object sender, EventArgs e)
{
if (dlCategory.SelectedIndex != -1)
{
string Category= dlCategory.SelectedItem.Text.ToString();
(lvProduct.FindControl("lblCategory") as Label).Text = "Category: " + dlCategory.SelectedItem.Text.ToString();
}
else
{
(lvProduct.FindControl("lblCategory") as Label).Text = "All category";
}
}

Related

CheckBox handling oncheck changed in ASP.net

i had a DataList view which i add a check box in the Item Template
i want each item i select to increase some counter for examble once it's checked ..
i used the following code to handle that ,but the event function is never accessed ?!
protected void selectItemCheckBox_CheckedChanged(object sender, EventArgs e)
{
int selected = 0;
foreach (DataListItem i in DataList1.Items)
{
CheckBox chk = (CheckBox)i.FindControl("selectItemCheckBox");
if (chk.Checked)
{
selected++;
}
selectedItemCount.Text = Convert.ToString(selected);
}`
}
Currently you're looping over every checkbox for every checked checkbox which is inefficient and depending on your other code, may be causing trouble.
You're better off incrementing for each checkbox individually.
...DataList...
<ItemTemplate>
<asp:CheckBox id="selectItemCheckBox" runat="server"
AutoPostBack="True"
OnCheckedChanged="selectItemCheckBox_CheckedChanged" />
</ItemTemplate>
...DataList...
After a box is checked, update the total for just that checkbox using sender
protected void selectItemCheckBox_CheckedChanged(object sender, EventArgs e)
{
// Parse the total selected items from the TextBox.
// You may consider using a Label instead, or something non-editable like a hidden field
int totalChecked;
if (int.TryParse(selectedItemCount.Text, out totalChecked) = false)
totalChecked = 0;
// Get a reference to the CheckBox
CheckBox selectItemCheckBox = (CheckBox)sender;
// Increment the total
if (selectItemCheckBox.Checked == true)
totalChecked++;
// Put back in the TextBox
selectedItemCount.Text = totalChecked.ToString();
}

how to display selected value from radiobuttonlist

I tried
Dim theme = rblTheme.Items(rblTheme.SelectedIndex).Value
getting first value selected
Dim theme = rblTheme.SelectedItem.Value
getting first value selected
Dim theme = ""
For i As Integer = 0 To rblTheme.Items.Count - 1
If (rblTheme.Items(i).Selected) Then
theme = rblTheme.Items(i).Value
End If
Next
Getting both value selected.
Need help !!
Dim selectedItemVal as string
selectedItemVal=rblTheme.SelectedValue;
I guess you are reloading the RadioButton list in the page postback again (when you click on some button). So probably you should move that code If isPostBack property is false
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
//Your code to load the Radio button list control
}
}
Now you should be able to get the value on your button click event.

Find a GridView row programmatically

The row has a LinkButton that when clicked needs to highlight the row.
Code so far:
protected void linkbutton1_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
//g.Style etc etc
}
first of all set the "CommandName" property of LinkButton to "select",
then in the selectedIndexChanging event of gridview write below code:
for (int i = 0; i < GridView1.Rows.Count;i++ )
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;
Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.
Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:
Select Case e.CommandName
Case "HighlightRow"
e.item.row.attributes("class") = "highlight"
End Select
Sorry its in VB.NET and not C#
1.) Set Command Name Property to "Select"
2.) Change style either on code behind as shown by #Raymond or give set Cssclass attribute for
SelectedRowStyle of gridview to CssClass="selecterowstyle"
.selectedRowstyle
{
background-color:#EAEAEA;
}

ASP.Net - Gridview row selection and effects

I'm using the following code in attempt to allow the user to select a gridview row by clicking anywhere on the row (plus mouse over and out) effects. The code doesn't seem to be applied on rowdatabound and I can't break into the event. (It is wired).
The control is in a usercontrol, that lives in a content page, which has a masterpage.
protected void gvOrderTypes_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridView gvOrdTypes = (GridView)sender;
//check the item being bound is actually a DataRow, if it is,
//wire up the required html events and attach the relevant JavaScripts
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "javascript:setMouseOverColor(this);";
e.Row.Attributes["onmouseout"] = "javascript:setMouseOutColor(this);";
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvOrdTypes, "Select$" + e.Row.RowIndex);
}
}
Scratch that. The handler was wired to the databound event instead of rowdatabound. Sorry everyone. As a side note, the code works

Problem getting commandeventargs in command event following re-creationg of controls in postback

I am haveing problems getting the command event args following the second click using the code below.
so - when i process a button click, and generate a new button to replace the one that was there i lose the viewstate on the next button click.
Any suggestions on what I need to do to get this to work? I cannot significantly change the structure as I must generate a variable number of totally un-related buttons in the command handler.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
LinkButton btn = new LinkButton();
btn.ID = "btn1";
this.Panel1.Controls.Add(btn);
btn.Command += new CommandEventHandler(myLinkButton_Command);
}
else
{
LinkButton btn = new LinkButton();
btn.ID = "btn1";
this.Panel1.Controls.Add(btn);
btn.Text = "My Button 1";
btn.CommandArgument = "1";
btn.Command += new CommandEventHandler(myLinkButton_Command);
}
}
void myLinkButton_Command(object sender, CommandEventArgs e)
{
int newArg = Convert.ToInt32(e.CommandArgument) + 1;// empty string on second mouse click
this.Panel1.Controls.Clear();
LinkButton myLinkButton = new LinkButton();
myLinkButton.ID = "btn1";
this.Panel1.Controls.Add(myLinkButton);
myLinkButton.Text = "My Button " + newArg.ToString();
myLinkButton.CommandArgument = newArg.ToString();
}
}
This happens because your panel has a literal control in it. When you add your button the first time, it (the button) is a second control. When you later clear panel's controls collection, it becomes the first control and the viewstate is saved for the first control, which on following postback becomes the literal.
Simply convert
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
to
<asp:Panel ID="Panel1" runat="server" />
and it will work.
You have forgotten to set the CommandArgument property when you recreate the button in Page_Load.

Resources