DropDownList中的选定值在选择其他内容时也始终返回默认值

时间:2017-07-18 11:36:17

标签: asp.net vb.net dropdown selectedvalue

我正在使用ASP.NET。我有一个动态构建的Dropdownlist。现在,当您点击提交按钮时,我只会在您从下拉列表中选择某些内容时尝试执行某些操作(如果值不是0'则为')。但不知何故,我的值总是返回0(默认值),当选择了某些内容并且值应为1或2时。

这里有谁知道可能出现什么问题?

在这里,我循环遍历我的所有下拉列表,每个下拉列表都有一个电子邮件输入(这两个属于一起)

            For i As Integer = 0 To allDropDownLists.Count - 1
                If allDropDownLists(i).SelectedValue = "0" Then
                Else
                    If allEmailInputs(i).Text IsNot "" Then
                        final &= saveEventUser(myID, allDropDownLists(i).SelectedValue, allEmailInputs(i).Text)
                    Else
                        error.InnerHtml = "error!!"
                    End If
                End If
            Next

这就是动态构建所有内容的方式:

            Dim div71 As New Panel
            div71.CssClass = "border"
            Dim ddl As New DropDownList
            ddl.ID = "select" & panel & "_" & counter
            ddl.Items.Clear()
            ddl.Items.Add(New ListItem("Select something", "0"))
            ddl.Items.Add(New ListItem("Select 1", "1"))
            ddl.Items.Add(New ListItem("Select 2", "2"))
            ddl.CssClass = "form_txt2 colegas"

1 个答案:

答案 0 :(得分:0)

使用visibile =" false"以标准ASP.NET方式创建下拉列表,然后填充时将其可见性设置为true。

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="BatchID" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    <Columns>
        <asp:BoundField DataField="Column1" HeaderText="Column1"  SortExpression="Column1"  />
        <asp:BoundField DataField="Column2" HeaderText="Column2" SortExpression="Column2" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

否则,您可以构建一个GridView,其中每行都有一个DropDownList,因此您可以控制要显示的行数(因此 - > DropDownLists) - 如果将此GridView绑定到具有5条记录的数据集,您将有5个DropDownLists。

public class OptionMenuFragment extends Fragment implements View.OnClickListener{


private String phoneNumber, email;

public OptionMenuFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.option_main_fragment, container, false);

    Button myColleagues=(Button) view.findViewById(R.id.myColleagues);
    myColleagues.setOnClickListener(this);
    ......
    return view;

}

@Override
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.myColleagues:
            Intent intent = new Intent(getActivity(), MyColleaguesPage.class);
            startActivity(intent);
            break;
        case R.id.news:
           ......
            break;
    }
}

private void showFirstDialog() {

    //Create a new builder and get the layout.
    final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
   ......

    alertButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alert.dismiss();
        }
    });

    alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // ListView Clicked item index
            int itemPosition = position;

            if (itemPosition == 0) {
                alert.dismiss();
                showSecondDialog(0);
            }

           .......
            else if (itemPosition == 5) {
                alert.dismiss();
                showSecondDialog(5);
            }
        }
    });
}

private void showSecondDialog(int position) {

    alertTitle.setText("What do you want to do");
    // Defined Array values to show in ListView
    String[] values = getResources().getStringArray(R.array.contact_way);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(),
            R.layout.first_alertlist_textstyle, android.R.id.text1, values);
    alertListView.setAdapter(adapter);

    alertButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            alert.dismiss();
        }
    });
    switch (position) {
        case 0:
            phoneNumber = "123456";
            email="mail.com";
            break;
       ......
        case 5:
            phoneNumber = "+1233";
            email="mail.com";

    }
    alertListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // ListView Clicked item index
            int itemPosition = position;


            if (itemPosition == 0) {

                alert.dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                builder.setTitle("calling to " + phoneNumber);
                //builder.setMessage("Are you sure you want to log out?");

                builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // close the dialog, go to login page
                        if (isPermissionGranted()) {
                            call_action(); //here I am calling call method
                        }
                    }
                });
                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do nothing
                        dialog.dismiss();
                    }
                });

                AlertDialog alert = builder.create();
                alert.show();

            }
            if (itemPosition == 1) {
                ..

            }
            if (itemPosition == 2) {
             ....
}

//This code I want to implement into seperate class

 public void call_action(){

    Intent callIntent = new Intent(Intent.ACTION_CALL);
    callIntent.setData(Uri.parse("tel:" + phoneNumber));
    startActivity(callIntent);
 }
  public  boolean isPermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (getActivity().checkSelfPermission(android.Manifest.permission.CALL_PHONE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("TAG","Permission is granted");
            return true;
        } else {

            Log.v("TAG","Permission is revoked");
            ActivityCompat.requestPermissions(this.getActivity(), new String[]{Manifest.permission.CALL_PHONE}, 1);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v("TAG","Permission is granted");
        return true;
    }
}

 @Override
 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getActivity().getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                call_action();
            } else {
                Toast.makeText(getActivity().getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

您将在GridView RowDataBound事件上填充这些DropDownLists